Digiaru

Digiaru started this conversation 3 months ago.

Missing Cleanup in useEffect: Leaking Timers or Event Listeners

I register a window event listener inside useEffect, but never remove it. This causes memory leaks and duplicate behavior when the component unmounts and remounts.

Kar

Posted 3 months ago

Without cleanup, listeners persist beyond the component lifecycle, leading to memory leaks or duplicated handlers ([turn0search5]turn0search3]). Fix: jsx Copy code useEffect(() => { const onResize = () => { /* logic */ }; window.addEventListener('resize', onResize); return () => window.removeEventListener('resize', onResize); }, []);