Run a callback when the component unmounts. Use for cleanup and teardown.
import { useEffect } from 'react';
export function useUnmount(fn: () => void): void {
useEffect(() => {
return fn;
}, []);
}Runs the callback when the component unmounts. Use for cleanup: cancel subscriptions, abort requests, clear timers.
Return cleanup from the effect; useUnmount keeps the API explicit.
useUnmount(() => { subscription.unsubscribe(); });