Answer the question
In order to leave comments, you need to log in
What causes an error on setState?
Good evening. Please help me to fix the error.
The error occurs when I hang on onClick setState.
Here is the link to the component : https://pastebin.com/AeBqKL66
Error : Maximum update depth exceeded. This can happen when a component repeatedly calls setState inside componentWillUpdate or componentDidUpdate. React limits the number of nested updates to prevent infinite loops.
Thank you!
Answer the question
In order to leave comments, you need to log in
In addition to the problem you already encountered, there is another problem in your code: you should not access the current state inside the setState function, because this function is asynchronous, which means there is no guarantee that this.state will be the same at the time the state is applied. It sounds complicated, but it's actually not very simple. Here's a simple sandbox example:
https://jsfiddle.net/n5u2wwjg/147060/
Note that "-2" works as expected. And "+2" - no.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question