Created
September 20, 2021 13:44
-
-
Save ivillamil/eca7a62f2f208964345e46cd3a6b562a to your computer and use it in GitHub Desktop.
Effect hook that executes only after all components have been rendered
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
import {useEffect, useRef} from 'react' | |
const useEffectAfterMount = ( effect, deps ) => { | |
const isMounted = useRef(false) | |
useEffect(() => { | |
if (isMounted.current) { | |
return effect() | |
} else { | |
isMounted.current = true | |
} | |
}, [deps]) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment