Skip to Content
ohooks

useUnmount

Executes a cleanup callback when the component unmounts, always using the latest function reference.

Loading preview...

The useUnmount hook runs a callback when the component unmounts from the DOM, reading the latest props and state via useLatest so your cleanup logic never suffers from stale closures.

Installation

pnpm dlx shadcn@latest add https://ohooks.vercel.app/r/useUnmount.json

Usage

Examples

Saving Unsaved Draft State on Unmount

Persist the latest form input value when the user navigates away and unmounts the component.

Reporting Analytics View Duration

Record how far the user progressed before closing a view.