Open yairEO opened 1 year ago
useDepEffect
Runs a useEffect only when the dependency array changes and not on initial mount. (see related stackoverflow discussion)
useEffect
Instead of manually using a ref to know when the component mounted:
ref
const isMountedRef = useRef(); useEffect(() => { if (isMountedRef.current) document.title = `You clicked ${count} times`; isMountedRef = true; }, [count]);
Use the custom hook (much shorter):
useDepEffect(() => { document.title = `You clicked ${count} times`; }, [count]); // Only re-run the effect if count changes
useDepEffect
(use dependency effect)Runs a
useEffect
only when the dependency array changes and not on initial mount. (see related stackoverflow discussion)Instead of manually using a
ref
to know when the component mounted:Use the custom hook (much shorter):