Skip to content

Instantly share code, notes, and snippets.

@tyom
Created February 13, 2021 15:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tyom/d7841d216d08df490185287c71ce0367 to your computer and use it in GitHub Desktop.
Save tyom/d7841d216d08df490185287c71ce0367 to your computer and use it in GitHub Desktop.
useEffect which runs after component is mounted
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