One does not simply “setState”

The situation

A little recap…

class Counter extends React.Component {
state = {
event: 0,
clicks: 0
}
componentDidMount() {
monitoring(...).subscribe(() => this.setState({
event: this.state.value + 1
}));
}
handleButtonClick = e => this.setState({
clicks: this.state.clicks + 1
})
render() {
// ...
}
}

Why?

Introducing the setState with callbacks

this.setState((currentState) => ({
click: currentState.click + 1
});

Reusable setState

Computer Whisperer. Open-source contributor. Find me at https://amitosh.in/

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store