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>
);
}
答案 0 :(得分:1)
当您必须更新组件的状态时,react组件具有一个setState()方法,您可以使用该方法进行更新。
答案 1 :(得分:0)
要更改组件的状态,必须使用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