my question seems to be quite simple. But in the code below, how is it possible to use "prevState" as function without coding the function ?
handleAddOne() {
this.setState(prevState => {
return {
counter: prevState.counter + 1
};
});
}
` Here you can see all the code in which handleAddOne() is include.
import React from "react";
import ReactDOM from "react-dom";
class Counter extends React.Component {
constructor(props) {
super(props);
this.state = {
counter: 0
};
this.handleAddOne = this.handleAddOne.bind(this);
}
handleAddOne() {
this.setState(prevState => {
return {
counter: prevState.counter + 1
};
});
}
render() {
return (
<div>
<div>Counter: {this.state.counter}</div>
<button onClick={this.handleAddOne}>Add One</button>
</div>
);
}
}
const rootElement = document.getElementById("root");
ReactDOM.render(<Counter />, rootElement);
I didn't try anything special, just need explanation.