# useUnmount > Runs a cleanup function once, when the component unmounts. - Category: Effects - Package: `hookli` (install with `npm i hookli`) - Docs: https://hookli.vercel.app/docs/use-unmount ## Signature ```ts useUnmount(fn: () => void): void ``` ## Parameters - `fn`: `() => void` — Called exactly once when the component unmounts. The latest closure is captured in a ref, so it always sees fresh values. ## Usage ```tsx import { useUnmount } from "hookli"; export function Demo() { useUnmount(() => { console.log("cleanup on unmount"); }); return

Watch the console when I unmount.

; } ``` ## Source `src/hooks/use-unmount/use-unmount.ts` ```ts import { useEffect, useRef } from "react"; export const useUnmount = (fn: () => void) => { const fnRef = useRef(fn); // The latest closure every render, but only invoked on unmount. fnRef.current = fn; useEffect(() => () => fnRef.current(), []); }; ```