简体   繁体   English

计数器应用无法在React上运行

[英]Counter app doesn't work on React

I created 2 buttons and wanna them increase their value clicking on each button. 我创建了2个按钮,并希望它们增加每个按钮上的值。 The code below doesn't work. 下面的代码不起作用。 Please, help ! 请帮忙 ! All this code I render in html tag with class buttons . 我用类buttonshtml标记中呈现的所有这些代码。

class Buttons extends React.Component {
  state = { score_1: 0, score_2: 0 };

  updateScore_1() {
    this.setState((prevState) => {
      score_1: prevState.score_1 += 1
    });
  }
  updateScore_2() {
    this.setState((prevState) => {
      score_2: prevState.score_2 += 1
    });
  }
  render() {
    return (
      <div>
        <button onClick={this.updateScore_1.bind(this)}>{this.state.score_1}</button>
        <br />
        <button onClick={this.updateScore_2.bind(this)}>{this.state.score_2}</button>
      </div>
    )
  }
}
ReactDOM.render(
  <Buttons />,
  document.querySelector('.buttons')
);

You missed the keyword return in this.setState. 您错过了this.setState中的关键字return。 Just add return in this.setState : 只需在this.setState添加return this.setState

updateScore_2() {
  this.setState((prevState) => {
    return { score_2: prevState.score_2 + 1 }
  });
}

You should write score_1: prevState.score_1 + 1 instead of score_1: prevState.score_1 += 1 . 您应该写score_1: prevState.score_1 + 1而不是score_1: prevState.score_1 += 1 In your case you are mutating the state, however using the setState callback you need to return the updated state 在您的情况下,您正在改变状态,但是使用setState回调您需要返回更新的状态

this.setState((prevState) => ({   // notice the `({` here
  [key]: prevState[key] +1
}));

Also you can simply use one handler instead of two 您也可以只使用一个处理程序,而不是两个

 class Buttons extends React.Component { state = { score_1: 0, score_2: 0 }; updateScore(key) { this.setState((prevState) => ({ [key]: prevState[key] +1 })); } render() { return ( <div> <button onClick={this.updateScore.bind(this, 'score_1')}>{this.state.score_1}</button> <br /> <button onClick={this.updateScore.bind(this, 'score_2')}>{this.state.score_2}</button> </div> ) } } ReactDOM.render( <Buttons />, document.querySelector('.buttons') ); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script> <div class="buttons"/> 

Your issue I guess was from the fact that in the setState methods your were not returning the new state which should have been passed to the setState method 我猜您的问题是由于这样的事实,在setState方法中,您没有返回应该传递给setState方法的新状态

  updateScore_1() {
    this.setState((prevState) => {
      score_1: prevState.score_1 + 1
    });
  }

A couple of parentheses solves your issue or you can just put a return in from of the new state object. 括号可以解决您的问题,或者您可以将新状态对象的return放进去。 See complete solution below: 请参阅下面的完整解决方案:

class Buttons extends React.Component {
  constructor(props) {
    super(props);
    this.state = { score_1: 0, score_2: 0 };
    this.updateScore_1 = this.updateScore_1.bind(this);
    this.updateScore_2 = this.updateScore_2.bind(this);
  }

  updateScore_1() {
    this.setState(prevState => ({
      score_1: (prevState.score_1 += 1)
    }));
  }
  updateScore_2() {
    this.setState(prevState => ({
      score_2: (prevState.score_2 += 1)
    }));
  }
  render() {
    return (
      <div>
        <button onClick={this.updateScore_1}>{this.state.score_1}</button>
        <br />
        <button onClick={this.updateScore_2}>{this.state.score_2}</button>
      </div>
    );
  }
}

ReactDOM.render(
  <Buttons />,
  document.querySelector('.buttons')
);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM