Stop Rebuilding UI

Shadcn.io is not affiliated with official shadcn/ui

React useUnmount Hook

React useUnmount hook for cleanup on component unmount. Fresh callback via ref, no stale closures. TypeScript ready.

Run cleanup logic when a component unmounts with useUnmount. Pass a function and it executes exactly once when the component leaves the DOM. The callback is stored in a ref and updated every render, so it always has access to the latest props and state. No stale closure issues like raw useEffect cleanup. Throws immediately if passed a non-function to catch errors early. Uses empty dependency array internally—cleanup only runs on actual unmount, not re-renders. Perfect for analytics tracking, closing connections, clearing timers, releasing resources, or any teardown logic. SSR safe, works with Next.js App Router.

React useUnmount Hook preview

Scroll to load preview

Installation

FAQ

Was this page helpful?

Sign in to leave feedback.