Skip to content

Instantly share code, notes, and snippets.

@pie6k
Created March 13, 2019 22:38
Show Gist options
  • Save pie6k/02bf25d2603f9733fcd0d8741d679357 to your computer and use it in GitHub Desktop.
Save pie6k/02bf25d2603f9733fcd0d8741d679357 to your computer and use it in GitHub Desktop.
import { useEffect, useRef } from 'react';
export function useUnmountEffect(callback: () => void) {
const callbackRef = useRef(callback);
callbackRef.current = callback;
useEffect(() => {
return () => {
const callbackFunction = callbackRef.current;
if (!callbackFunction) {
return;
}
callbackFunction();
};
}, []);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment