UNPKG

beautiful-react-hooks

Version:

A collection of beautiful (and hopefully useful) React hooks to speed-up your components and hooks development

20 lines (19 loc) 663 B
import { useEffect, useRef } from 'react'; import createHandlerSetter from './factory/createHandlerSetter'; /** * Returns a callback setter for a callback to be performed when the component will unmount. */ const useWillUnmount = (callback) => { const mountRef = useRef(false); const [handler, setHandler] = createHandlerSetter(callback); useEffect(() => { mountRef.current = true; return () => { if (handler && handler.current && typeof handler.current === 'function' && mountRef.current) { handler.current(); } }; }, []); return setHandler; }; export default useWillUnmount;