Created
February 13, 2021 15:08
-
-
Save tyom/d7841d216d08df490185287c71ce0367 to your computer and use it in GitHub Desktop.
useEffect which runs after component is mounted
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 'preact/hooks'; | |
export function useMountedEffect(cb: () => void, deps?: ReadonlyArray<unknown>): void { | |
const didMount = useRef(false); | |
useEffect(() => { | |
if (didMount.current) cb(); | |
else didMount.current = true; | |
}, deps); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment