Created
May 23, 2020 18:41
-
-
Save yairEO/e617c9b4329c8a9d46e401ffcf12ffc9 to your computer and use it in GitHub Desktop.
React useEffect - disable execution on first load
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, { useEffect, useRef } from 'react'; | |
const useDidMountEffect = (func, deps) => { | |
const didMount = useRef(false); | |
useEffect(() => { | |
if (didMount.current) func(); | |
else didMount.current = true; | |
}, deps); | |
} | |
export default useDidMountEffect; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usage example: