Hello. Sorry for my English, I’ve just learning it.
I’m reading the book about React JS. Thanks a lot to Kirupa for it!
In the section 10 (about React events) there is an example of counter with button “+”: If I click on the button, the number in div is increase by 1, and if the Shift key is pressed the number increase by 10. I’m trying to add addition functionality: button “-” with same behavior but with decrease. But this code doesn’t working. If I press in “+” or “-” the number increase by 1, then by 10, and then buttons doesn’t work any more. What the main problem with my code? Can anybody help to me? Thanks a lot!
class CounterParent extends React.Component {
constructor(props) {
super(props);
this.state = {
count: 0
};
...
changeCount(newCount){
this.setState({
count: newCount
});
}
increase(e) {
this.changeCount(this.state.count + e.shiftKey?10:1);
}
decrease(e) {
this.changeCount(this.state.count - e.shiftKey?10:1);
}
...
return (
<div style={backgroundStyle}>
<Counter display={this.state.count} />
<button onClick={this.increase} style={buttonStyle}>+</button>
<button onClick={this.decrease} style={buttonStyle}>-</button>
</div>
);