Facebook Pixel

What Is useEffect in React and How Does It Work?

useEffect is the hook for side effects. Here is what it does, how the dependency array controls it, and the mistakes beginners make.

What Is useEffect in React and How Does It Work?

useEffect is the hook that handles side effects in functional components: data fetching, subscriptions, DOM manipulation, timers. It is also where most beginners get stuck.

What useEffect Does

useEffect runs a function after the component renders. That function can perform side effects and optionally return a cleanup function that runs before the next effect or on unmount.

The Dependency Array

The second argument to useEffect controls when it runs. No array means it runs after every render. An empty array means it runs only on mount. An array with values means it runs when any of those values change.

Why This Matters

The dependency array is the core of useEffect. Get it wrong and you get either an effect that never runs, or one that runs infinitely. Most useEffect bugs trace to a wrong dependency array.

Cleanup

If your effect sets up a subscription, listener, or timer, return a cleanup function. React calls it before the next effect and on unmount, preventing memory leaks and stale updates.

The Mental Model

Think of useEffect as 'after rendering, do this, and here is when to do it again.' The render produces the UI; the effect handles anything that reaches outside the component.

The Common Mistakes

Forgetting the dependency array causes infinite loops. Putting state you set inside the effect into the array also causes loops. Forgetting cleanup causes leaks.

The Takeaway

useEffect runs side effects after render, and the dependency array controls when it runs again. Always handle cleanup, and treat the dependency array as the most important part of the hook.

useEffect runs a function after the component renders to perform side effects like data fetching, subscriptions, and DOM manipulation. It can optionally return a cleanup function that runs before the next effect or on unmount.

It controls when the effect runs. No array means it runs after every render. An empty array means it runs only on mount. An array with values means it runs when any of those values change.

Usually because you forgot the dependency array, or because you included a state variable that you update inside the effect. Each update triggers another run. Fix the dependency array so the effect only runs when intended.

Whenever the effect sets up something that needs tearing down, like a subscription, event listener, or timer. Return a cleanup function so React can remove it before the next effect and on unmount, preventing memory leaks and stale updates.

Think of it as 'after rendering, do this, and here is when to do it again.' The render produces the UI; the effect handles anything that reaches outside the component, like fetching or subscribing.

Ready to master React completely?

Want to upskill yourself, crack your next interview, and get your dream job? Join our comprehensive course to dive deeper with high-quality video tutorials, solve interview questions, and a premium community.

Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.