Skip to content
usefy

useUnmount

Lifecycle & refs

A type-safe React hook that runs a callback when a component unmounts

Install

$ npm install @usefy/use-unmount

Quick start

useUnmount.tsx
import { useUnmount } from "@usefy/use-unmount";

function MyComponent() {
  useUnmount(() => {
    console.log("Component unmounted");
  });

  return <div>Hello</div>;
}

API reference

useUnmount(callback, options?)

Executes callback when the component unmounts. Returns void.

Parameters

ParameterTypeDefaultDescription
callback() => voidFunction to execute when the component unmounts
optionsUseUnmountOptions{}Optional configuration

UseUnmountOptions

PropertyTypeDefaultDescription
enabledbooleantrueWhether to run the callback on unmount, read at unmount time

Runs only on unmount: the enabled value is captured in a ref and read when the component actually unmounts. Flipping enabled from true to false (or back) while the component is still mounted never fires the callback.


Go deeper

This page is the quick reference. For every example, prop, and edge case, read the full README — or open Storybook to change props live.

More in lifecycle & refs