Last active
August 21, 2020 16:38
-
-
Save Landerson352/afd4f6f01907b7d5104cb7e44ab4126d to your computer and use it in GitHub Desktop.
A "componentWillUnmount" hook with dependencies.
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 React from 'react'; | |
// Usage: useOnUnmount((deps) => console.log(deps), deps); | |
const useOnUnmount = (fn, deps) => { | |
const ref = React.useRef(text); | |
React.useEffect(() => { | |
ref.current = deps; | |
}, [deps]); | |
React.useEffect(() => { | |
return () => fn(ref.current); | |
}, []); | |
}; | |
export default useOnUnmount; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment