Created
March 13, 2019 22:38
-
-
Save pie6k/02bf25d2603f9733fcd0d8741d679357 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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