In React I am trying to make a button increment a value stored in state. However using the code below function my value is set undefined or NaN when using handleClick.
Hello there, try these codes to increment your value
class Counter extends React.Component{
constructor(props){
super(props);
this.addOne = this.addOne.bind(this);
this.state = {
count : 0
}
}
addOne() { // addOne as HandleClick
this.setState((preState) => {
return {
count : preState.count + 1
};
});
}
render() {
return (
Count : {this.state.count}
);
}
}
ReactDOM.render( , document.getElementById('YOUR-ID'));