Last active
October 27, 2021 13:05
-
-
Save heytulsiprasad/34000e3b9e5ec22864a3122b5c14129c to your computer and use it in GitHub Desktop.
Custom hook that doesn't run on initial 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 { useRef, useEffect } from "react"; | |
const useNonInitialEffect = (effect, deps = []) => { | |
const initialRender = useRef(true); | |
useEffect(() => { | |
let effectReturns = () => {}; | |
if (initialRender.current) { | |
initialRender.current = false; | |
} else { | |
effectReturns = effect(); | |
} | |
if (effectReturns && typeof effectReturns === "function") { | |
return effectReturns; | |
} | |
}, deps); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment