Facebook Pixel

useEffect Dependency Array Mistakes That Cause Infinite Loops

The useEffect dependency array causes most hook bugs. Here are the common mistakes and how to fix each.

useEffect Dependency Array Mistakes That Cause Infinite Loops

The dependency array is the most error-prone part of useEffect. Here are the common mistakes and how to fix each.

Forgetting the Array

Without a dependency array, the effect runs after every render. If the effect sets state, it triggers another render, which runs the effect again, creating an infinite loop.

Including State You Update

If you set a state variable inside the effect and also include it in the dependency array, every update triggers another run, causing a loop.

Missing Dependencies

If you use a value inside the effect but do not include it, the effect uses a stale version of that value. This causes bugs that are hard to trace because the effect does not react to changes.

Objects and Functions as Dependencies

If you create a new object or function on every render and include it in the array, the effect runs on every render because the reference changes each time.

Using the Wrong Array

Sometimes you want the effect to run only on mount, but you include a value that changes, so it runs too often. Use an empty array when you truly mean mount-only.

The Fix

Include every value the effect uses that can change. Use an empty array for mount-only. For objects and functions, memoize them with useMemo and useCallback, or move them inside the effect.

The Safety Net

Enable eslint-plugin-react-hooks. It warns you about missing dependencies and helps you get the array right automatically.

The Takeaway

Most useEffect bugs come from a wrong dependency array: forgetting it, including state you update, missing dependencies, or using new references each render. Get the array right and most hook bugs disappear.

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

The effect uses a stale version of that value and does not react when it changes. This causes bugs that are hard to trace because the code looks correct but behaves wrong. The hooks lint plugin catches these.

Because you included an object or function that is recreated on every render. The reference changes each time, so the effect runs every render. Memoize the value or move it inside the effect.

Use an empty dependency array. This tells React to run the effect only once after the initial mount and clean it up on unmount. Be sure you truly mean mount-only and are not skipping a real dependency.

Include every value the effect uses that can change, use an empty array for mount-only, memoize objects and functions with useMemo and useCallback, and enable eslint-plugin-react-hooks to catch mistakes automatically.

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.