Answer the question
In order to leave comments, you need to log in
How to state +/- 1?
How to manage the State value in this case? the task is simple, you need to contain a number and -/+ 1 buttons, while displaying it on the page. Both options don't work.
eng_cat_ackyes: 0,
eng_cat_ackno: 0,
...
<p>Acknowledged</p>
<Row>
<Button onChange={()=>this.setState({ eng_cat_ackyes: ++this.state.eng_cat_ackyes})}> + </Button>
<Input value={this.state.eng_cat_ackyes}
onChange={(event)=>this.setState({ eng_cat_ackyes: event.target.value})}
type="text" />
<Button onChange={()=>this.setState({ eng_cat_ackyes: --this.state.eng_cat_ackyes})}> - </Button>
</Row>
<p>Not Acknowledged</p>
<Row>
<Button onChange={()=>this.setState({ eng_cat_ackyes: this.state.eng_cat_ackyes+1})}> + </Button>
{this.state.eng_cat_ackno}
<Button onChange={()=>this.setState({ eng_cat_ackyes: this.state.eng_cat_ackyes-1})}> - </Button>
</Row>
Answer the question
In order to leave comments, you need to log in
you are trying to change the state, the state in React must be treated as if it is immutable, that is, you cannot set the state based on the previous state + something, because you cannot change the state itself, i.e. this.state.number++ won't work
https://jsfiddle.net/k1wukhzq/
The secret is out. We change onChange to onClick and everything works in the original version
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question