| 1 |
import { useEffect, useRef } from 'react'; |
| 2 |
/** |
| 3 |
* A `useEffect`-like hook that only triggers when a component unmounts. Does not require a dependency list, as the effect callback will always be kept up to date. |
| 4 |
*/ |
| 5 |
export function useUnmountEffect(effect) { |
| 6 |
// Always use the latest effect callback so that it can reference the latest props and state. |
| 7 |
const effectRef = useRef(effect); |
| 8 |
effectRef.current = effect; |
| 9 |
// Trigger the effect callback when the component unmounts. |
| 10 |
useEffect(() => () => { |
| 11 |
effectRef.current(); |
| 12 |
}, []); |
| 13 |
} |
| 14 |
//# sourceMappingURL=useUnmountEffect.js.map |