Created
November 16, 2020 08:45
-
-
Save mkhoussid/c08d0f0dbf597422559910c2fdc60d42 to your computer and use it in GitHub Desktop.
useComponentWillMount ReactJS hook
This file contains hidden or 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
// NOTE: This does NOT work on NextJS: https://github.com/vercel/next.js/discussions/19204 | |
// Adapted from here: https://stackoverflow.com/questions/53464595/how-to-use-componentwillmount-in-react-hooks#comment101488609_56818036 | |
const useComponentWillMount = (func: (...params: any[]) => any): void => { | |
const willMount = useRef(true); | |
useEffect(() => { | |
// invert flag to ensure `func` does not run twice after mount | |
willMount.current = false; | |
}, []); | |
if (willMount.current) func(); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment