React SetState

in the process of using react-swipe, you want to assign the index of the current tab to the variable in state in the callback method

clipboard.png

clipboard.png
setState

clipboard.png

clipboard.png
the output is wrong. Can someone help me to see how to solve it?

Mar.11,2022

your state value had better not be changed in the render function


do not setState in render , otherwise it is easy to cause an endless loop.


the specific reason is that the opt definition is placed in the render function. Every time the broadcast chart is updated, render is re-executed, so opt is reset, so it is really best not to setState, in the render method. So I defined


in componentDidMount because the this is incorrect. You can only use the this bind of the method in constructor once to change the location of your code, but the direction of this is wrong. You need to take your method outside render. Then use this.yourfunctionname=this.yourfunctionname.bind (this); So you can use this.setstate

in your yourfunctionname.
Menu