Back to articles
🧹 Why Cleanup Functions Matter in useEffect

🧹 Why Cleanup Functions Matter in useEffect

via Dev.to ReactAman Kureshi

When using useEffect, sometimes you need to clean up resources like timers, subscriptions, or event listeners. 📘 Example: import { useEffect } from " react " ; function Timer () { useEffect (() => { const timer = setInterval (() => { console . log ( " Running... " ); }, 1000 ); return () => clearInterval ( timer ); // cleanup }, []); } ✨ Key Points: • Cleanup prevents memory leaks • Runs when the component unmounts • Also runs before the effect executes again Always clean up side effects to keep your React apps efficient and bug-free. 🚀

Continue reading on Dev.to React

Opens in a new tab

Read Full Article
4 views

Related Articles