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:
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
useEffectonly when the dependency array changes and not on initial mount.(see related stackoverflow discussion)
Instead of manually using a
refto know when the component mounted:Use the custom hook (much shorter):