Best Division

Spread Your Knowledge

  • All topics
  • About us
  • Contact us
  • Privacy policy
  • Terms of service
  • Copyright
  • Facebook
  • Twitter
  • Instagram
  • Youtube
  • LinkedIn
  • Pinterest
  • Github
  • Tumblr

© 2026 Best Division

How Do You Fix an Infinite Loop in useEffect?

ReactJavaScript
Jayant Kumar
Jayant Kumar@jayantkumar314
PublishedDec 6, 2026
6 min read

A useEffect infinite loop happens when an effect changes state or a value that is also in its dependency array, so every run triggers a re-render that triggers the effect again. To fix it, find which dependency changes on every render, then stabilise it, remove it from the effect, or stop setting state unconditionally.

The three classic causes are a missing dependency array, state that is both updated by and listed in the effect, and objects, arrays or functions created during render that are used as dependencies. This guide shows each one with a fix.

Table of Contents

  • How a useEffect infinite loop starts
  • Cause 1: no dependency array
  • Cause 2: updating state that is listed as a dependency
  • Cause 3: objects, arrays and functions in dependencies
  • Cause 4: setting state with a fresh array or object from props
  • A debugging checklist
  • Frequently Asked Questions
    • Is an empty dependency array always safe?
    • Why does my fetch in useEffect run forever?
    • Can useEffect cause an infinite loop without setState?
    • Should I disable the exhaustive-deps lint rule?
  • Summary
  • Related Articles

How a useEffect infinite loop starts

React re-runs an effect after a render whenever any value in its dependency array differs from the previous render, compared with Object.is. If the effect itself causes another render and the dependencies change again, the cycle never ends:

  1. The component renders.
  2. The effect runs and calls a state setter.
  3. The state change triggers a render.
  4. The dependencies look different, so the effect runs again.

In the browser this shows up as a frozen tab, a flood of network requests, or the console warning "Maximum update depth exceeded".

Cause 1: no dependency array

Without a second argument, the effect runs after every render:

jsx
jsx
Loading syntax highlighting...

Add a dependency array. If the effect should only run on mount, use an empty array:

jsx
jsx
Loading syntax highlighting...

If you only wanted to update the count in response to something, do it in an event handler instead of an effect.

Cause 2: updating state that is listed as a dependency

jsx
jsx
Loading syntax highlighting...

Two fixes work, depending on intent:

  • Use the functional update form and drop the dependency, when the effect should run once:
jsx
jsx
Loading syntax highlighting...
  • Add a condition so the update eventually stops changing state:
jsx
jsx
Loading syntax highlighting...

If a value can be calculated from props or other state, do not store it in state with an effect at all. Compute it during render.

Cause 3: objects, arrays and functions in dependencies

Objects, arrays and functions are new references on every render, even when their contents are the same:

jsx
jsx
Loading syntax highlighting...

Fixes, in order of preference:

  1. Depend on the primitive value instead: [role].
  2. Move the object or function inside the effect, so it is not a dependency.
  3. Move constant objects outside the component.
  4. Wrap the value in useMemo or the function in useCallback when it must be passed down.
jsx
jsx
Loading syntax highlighting...

React 19 with the React Compiler can memoise many of these values automatically, but you should still not rely on it to hide a bad dependency list.

Cause 4: setting state with a fresh array or object from props

jsx
jsx
Loading syntax highlighting...

Listing items, the state you are setting, makes it a loop. Remove it from the dependencies; the effect reads only props.items. Better still, derive it:

jsx
jsx
Loading syntax highlighting...

A debugging checklist

  1. Read the dependency array. Which value is created inside the component body?
  2. Log the dependencies in the effect, or use the React DevTools Profiler, to see which one changes.
  3. Check whether the effect sets state that it also depends on.
  4. Ask whether you need an effect at all, or whether the value can be calculated while rendering.
  5. Keep the react-hooks/exhaustive-deps lint rule on. Suppressing it with a comment is how many loops are introduced.

Frequently Asked Questions

Is an empty dependency array always safe?

It stops the loop, but it can read stale values. If the effect uses a prop or state, include it, or use a functional state update so you do not need it.

Why does my fetch in useEffect run forever?

Usually it sets state that changes a dependency, or a dependency is an object or function re-created every render. Depend on primitives and keep the fetch inside the effect.

Can useEffect cause an infinite loop without setState?

Yes, if it triggers a change in a parent through a callback prop and that parent passes a new object or function back down each time.

Should I disable the exhaustive-deps lint rule?

No. The warning usually points at the real cause. Restructure the code so the dependencies are honest and stable.

Summary

An infinite loop in useEffect means the effect keeps causing the render that re-runs it. Add a dependency array, depend on primitives instead of objects created during render, use functional state updates, and compute derived values during render instead of in effects. Keep the lint rule enabled to catch the next one early.

Related Articles

  • Why Does useEffect Run Twice in React Strict Mode?
  • When Do You Not Need useEffect in React?
  • Learn React 19: Complete Study Roadmap from Beginner to Advanced
Share

Similar posts

  • Why Does useEffect Run Twice in React Strict Mode?
  • How Do You Clean Up Side Effects in useEffect?
  • How Do You Run useEffect Only Once on Mount?
  • How Does the useEffect Dependency Array Work?
  • What Is useEffect in React and When Does It Run?
  • When Should You Use useReducer Instead of useState?
  • How Do You Use the useReducer Hook in React?
  • How Do You Reset Component State Using the key Prop?