Created
October 25, 2024 17:25
-
-
Save AsbDaryaee/b91768637e62074611e0bfb8aacaba6f to your computer and use it in GitHub Desktop.
React useEffect Hook that Skips First Render
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'; | |
function useSkipFirstRenderUseEffect(callback: () => void, dependencies: [any]) { | |
const firstRenderRef = useRef(true); | |
useEffect(() => { | |
if (firstRenderRef.current) { | |
firstRenderRef.current = false; | |
return; | |
} | |
callback(); | |
}, dependencies); | |
} | |
export default useSkipFirstRenderUseEffect; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment