Reputation: 305
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
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
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