简体   繁体   English

onClick不起作用, <button>但可以起作用</button>

[英]onClick doesn't work on <button> but work on <a>

trying to trigger an event onClick, it works when i use "a" tag but when i use button it doesn't : 尝试触发事件onClick,当我使用“ a”标签时它起作用,但是当我使用按钮时,它不起作用:

constructor(props) {
    super(props);
    this.backwardWeek = this.backwardWeek.bind(this);

    this.state = {
      displayedWeek: 0,
      inputs: []

    }; 
backwardWeek() {
    this.state.displayedWeek = this.state.displayedWeek - 1;   
   }

render(){

return (   
  <button onClick={this.backwardWeek}>  <span className="glyphicon glyphicon- 
   arrow-left" /></button> 
  );
    }

当您必须更新组件的状态时,react组件具有方法setState(),您可以使用该方法进行更新。

In order to change component's state, you have to use this.setState . 为了更改组件的状态,您必须使用this.setState

constructor(props) {
    super(props);
    this.backwardWeek = this.backwardWeek.bind(this);

    this.state = {
      displayedWeek: 0,
      inputs: []

    }; 
backwardWeek() {
    this.setState({
      displayedWeek: this.state.displayedWeek - 1
    }) // notice the difference
   }

render(){

return (   
  <button onClick={this.backwardWeek}>  <span className="glyphicon glyphicon- 
   arrow-left" /></button> 
  ); // notice the difference

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

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