Last active
March 21, 2022 17:18
-
-
Save Muzietto/c8822805231a90c2a13edb9b80f8bb85 to your computer and use it in GitHub Desktop.
useDelayedEffect - using setTimeout in React functional components (custom hook)
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 useDelayedEffect(effect, changingStateVars = [], delay = 1000) { | |
const mutable = useRef(); | |
const delayedEffect = () => { | |
mutable.current = setTimeout(effect, delay); | |
return () => { | |
clearTimeout(mutable.current); | |
}; | |
}; | |
useEffect(delayedEffect, changingStateVars); | |
} | |
export default useDelayedEffect; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment