Demystifying React useEffect: A Simple Guide
Oct 05, 2026 · 4 min read
Learn how to manage side effects, dependency arrays, and cleanup functions in React functional components.
The useEffect hook lets you perform side effects in your components, such as fetching data, updating the DOM, or setting up subscriptions.
Understanding the Dependency Array
- No Array: Runs after every single render.
- Empty Array
[]: Runs only once when the component mounts. - Array with Variables `[userId]*: Runs on mount and whenever any dependency value changes.
Cleanup Functions
If your effect sets up a timer or event listener, return a cleanup function to prevent memory leaks when the component unmounts:
useEffect(() => {
const timer = setInterval(() => console.log('Tick'), 1000);
return () => clearInterval(timer); // Cleanup
}, []);