在React中,我试图使按钮增加存储在状态中的值.但是,使用下面的代码函数时,我的值在使用handleClick时设置为undefined或NaN.
class QuestionList extends React.Component {
constructor(props) {
super(props);
this.state = {value: 0};
// This binding is necessary to make `this` work in the callback
this.handleClick = this.handleClick.bind(this);
}
handleClick = (prevState) => {
this.setState({value: prevState.value + 1});
console.log(this.state.value)
}
Run Code Online (Sandbox Code Playgroud)
你能告诉我为什么会这样吗?它应该是正确的根据这里的文档:https: //facebook.github.io/react/docs/state-and-lifecycle.html