use setState in a function code example
Example 1: setstate react js
constructor(props) {
super(props);
this.state = {
isActive: true,
};
}
checkStatus = () => {
this.setState({ // use this function
'isActive' : !this.state.isActive,
});
}
Example 2: passing function to setstate
//use this when current value of state depends on prevstate value For eg. toggle
this.setState(function(prevState){
return {show: !prevState.showForm}
});