Whether you need a high performance web application, fullstack architecture advisory, or an engineer who ships with urgency and care.
Jump directly on my calendar to talk about goals, timeline, and fit.
useEffect hook is one of the most powerful and misunderstood hooks. It handles side effects, runs after rendering, replaces lifecycle methods, and synchronizes your component with external systems such as APIs, subscriptions, DOM manipulation, timers, or logging. However, as your application grows, useEffect can become a source of bugs, race conditions, duplicate network calls, and performance regressions if not used correctly. This blog covers useEffect usage, best practices, patterns, and pitfalls that even experienced developers run into.
React's render function must be pure meaning it cannot modify variables outside its scope, perform I/O, or change the DOM. Anything that breaks purity is a side effect, for example:
Fetching data
Subscribing to WebSockets
Setting timers
Writing to localStorage
Manipulating the DOM
Syncing app state with URL or props
useEffect allows these operations in a safe, predictable way.
useEffect Runs After Render (Not During Render)
This guarantees:
The DOM is updated before the effect runs.
Effects never block rendering.
Effects run asynchronously.
The dependency array controls when the effect should re-run.
Run on every render
Run only once (on mount)
Equivalent to componentDidMount.
Run when dependencies change
Equivalent to componentDidUpdate(userId).
Run on unmount (cleanup)
Equivalent to componentWillUnmount.
Cleanup is essential for:
unsubscribing event listeners
aborting fetch requests
closing WebSockets
clearing intervals
canceling timeouts
Every time the effect re-runs, cleanup executes before running the next effect.
This code causes an infinite loop
Because setCount changes state -> triggers re-render -> triggers effect -> repeats forever.
Fix using dependencies
Beginners often write
This works but has problems
Cannot cancel outdated requests
Race conditions when dependency changes
Causes warnings in React Strict Mode
Use useEffect Only for Side Effects
Not everything requires an effect.
Bad (overusing effects)
Better (derive inside render)
Effects should not calculate derived state, they only handle side effects.
Every value used inside an effect must appear in its dependency array.
Example: Missing dependency
React will warn you.
Correct
Consider:
Here, count is always the initial value.
Solutions
Add count as dependency (causes interval resets)
Use functional state updates
Use refs
Improved version
componentDidMount -> useEffect(..., [])
componentDidUpdate -> useEffect(..., [deps])
componentWillUnmount -> useEffect(() => return cleanup, [])
getDerivedStateFromProps -> Not directly an effect (use memo/derived data)
Running effects too often
Fix: Use proper dependencies.
Using objects/arrays without memoization
Fix:
Fetching data incorrectly
Fix: Use AbortController or a library like SWR/React Query.
Using effects to perform calculations
Fix: Calculate inside render or use useMemo.
Why do effects run twice in React Strict Mode?
To detect unsafe side effects.
How do you avoid stale closure bugs inside effects?
Functional updates or refs.
Why must all dependencies be listed?
To maintain predictable behavior and avoid stale values.
What is the difference between an effect and layout effect?
useLayoutEffect runs before the browser paints. useEffect runs after the paint.
How do you cancel network requests inside an effect?
AbortController.
Avoid effects when:
You’re computing derived state
You can use CSS instead of JS to manipulate DOM
You’re syncing two pieces of state
Example of overkill
Better
useEffect is extremely powerful, but with great power comes great responsibility. Used correctly, it allows your app to sync with the outside world safely. Used incorrectly, it creates bugs, unnecessary renders, and spaghetti logic.
Focus on
Use correct dependencies
Perform cleanups
Avoid overusing useEffect
Understand async behaviour
Prevent stale closures
Explore more articles on software architecture, frontend craftsmanship, and backend engineering.



