N
N
Ninja Mate2016-03-02 01:46:50
JavaScript
Ninja Mate, 2016-03-02 01:46:50

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

2 answer(s)
M
MhMadHamster, 2016-03-02
@victorzadorozhnyy

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/

N
Ninja Mate, 2016-03-02
@victorzadorozhnyy

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 question

Ask a Question

731 491 924 answers to any question