Version 1: use = sign to assign value
countViewtimes: function(viewtimes){Final value of this.state.viewtimes : 0
this.state.viewtimes = this.state.viewtimes+viewtimes;
}
Since React can't listen to your updated state in this way. Your component is not able to be rendered. So nothing changes and state of viewtimes is still 0, the default value.
Version 2: using setState()
countViewtimes: function(viewtimes){final value of this.state.viewtimes : 56
this.setState({viewtimes:this.state.viewtimes+viewtimes});
},
setState() enqueues changes to the component state and tells React that this component and its children need to be re-rendered with the updated state.
However, using setState() with this method is not asychronous and might cause problem without sync state data. Final value of this.state.viewtimes is 56 but the result is not expected (expected result : 133+112+13+29+56 = 343).
Version 3: use setState() with updater function
countViewtimes: function(viewtimes){or
this.setState((prevState, props) => {
return { viewtimes: prevState.viewtimes + viewtimes }
});
},
countViewtimes: function(viewtimes){final value of this.state.viewtimes :343
this.setState((prevState) => {
return { viewtimes: prevState.viewtimes + viewtimes }
});
},
This is suggested way : taking a callback function with a new object based on the input from prevState and props(optional) as argument of setState(), rather than updating the state directly.
setState() runs asynchronously and does not always update the component immediately. In this case updates of state viewtimes are base on previous state, we need to use updater updates state which is based on prevState and props. Here is the updater signature:
(prevState, props) => stateChange
Reference
https://reactjs.org/docs/react-component.html#setstatehttp://lucybain.com/blog/2016/react-state-vs-pros/
No comments :
Post a Comment