Last active
November 30, 2019 17:08
-
-
Save PaulRBerg/8a249e5b7608c018607c63437772eba1 to your computer and use it in GitHub Desktop.
Custom Hook for delaying side effects declaratively
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
function useEffectWithDelay({ condition = false, func = () => {}, delay = 1000 }) { | |
useEffect(() => { | |
if (condition) { | |
const timeout = setTimeout(func, delay); | |
return () => { | |
clearTimeout(timeout); | |
}; | |
} | |
return undefined; | |
}, [condition, delay, func]); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You can now also find this in the npm registry, as part of the @paulrberg/react-hooks package. Install it as usual: yarn add @paulrberg/react-hooks or npm install @paulrberg/react-hooks.