user395817
user395817

Reputation: 305

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 :

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> 
  );
    }

Upvotes: 0

Views: 88

Answers (2)

Harshad Thombare
Harshad Thombare

Reputation: 81

When you have to update the state of component, react component has a method setState() which you can use to do so.

Upvotes: 1

Anton Harniakou
Anton Harniakou

Reputation: 880

In order to change component's state, you have to use 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

Upvotes: 0

Related Questions