Every effect that subscribes returns an unsubscribe
An effect that adds a listener without returning a cleanup adds another one on every re-run, and the old ones keep firing against unmounted components.
useEffect(() => {
const onResize = () => setWidth(window.innerWidth);
window.addEventListener("resize", onResize);
return () => window.removeEventListener("resize", onResize);
}, []);Strict mode mounts, unmounts and remounts on purpose in development so this bug shows up on the first render instead of after an hour.
react