Advertisement
Guest User

Untitled

a guest
Mar 18th, 2019
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.36 KB | None | 0 0
  1. import { useEffect, useRef } from 'react';
  2.  
  3. export function useUnmountEffect(callback: () => void) {
  4. const callbackRef = useRef(callback);
  5. callbackRef.current = callback;
  6.  
  7. useEffect(() => {
  8. return () => {
  9. const callbackFunction = callbackRef.current;
  10.  
  11. if (!callbackFunction) {
  12. return;
  13. }
  14.  
  15. callbackFunction();
  16. };
  17. }, []);
  18. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement