Calling setState in render causes infinite loop
See original GitHub issueThis 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:
- Created 8 years ago
- Comments:26 (3 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
I ran into this by accident once… I had
onClick={this.setState({inProgress: true})}
instead ofonClick={() => 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.The fact that we support calling
setState
incomponentDidMount
orcomponentDidUpdate
is not an accident – doing so in order to trigger an update is sometimes useful.