question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Calling setState in render causes infinite loop

See original GitHub issue

This may seem really silly to do to call setState in render. However it’s possible for this to happen if a component has a callback which is being called immediately during render and in your callback handler you call setState.

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:26 (3 by maintainers)

github_iconTop GitHub Comments

230reactions
jimbollacommented, Dec 3, 2015

I ran into this by accident once… I had onClick={this.setState({inProgress: true})} instead of onClick={() => this.setState({inProgress: true})}. It would be nice if React detected the problem and threw an error “setState detected in render() of SomeComponent” instead of going into the infinite loop.

19reactions
sophiebitscommented, Dec 5, 2015

Again this is an oversimplification and what Foo might be doing is dumb but react still shouldn’t call render immediately after setState in the same reconciliation cycle.

The fact that we support calling setState in componentDidMount or componentDidUpdate is not an accident – doing so in order to trigger an update is sometimes useful.

Read more comments on GitHub >

github_iconTop Results From Across the Web

setState method causes infinite loop of Results in Reactjs ...
You are calling in your render() method this.updateCount(person.id) which do a setState . Therefore a re-rendering occurs, and then this.
Read more >
3 ways to cause an infinite loop in React - Alex Sidorenko
I. Updating the state inside the render ... function App() { const [count, setCount] = useState(0); setCount(1); // infinite loop return ... }...
Read more >
Calling setState() with child functional component causes ...
Coding example for the question Calling setState() with child functional component causes infinite loop-Reactjs.
Read more >
How to Solve the Infinite Loop of React.useEffect()
1.1 Fixing dependencies. The infinite loop is fixed by correct management of the useEffect(callback, dependencies) dependencies argument.
Read more >
too many re-renders. react limits the number of ... - You.com
The reason for the infinite loop is because something (most likely setState ) in the event callback is triggering a re-render. This will...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found