Using a function in `setState` instead of an object

First, a little background

An example

this.state = { showForm : true}
submit(){
this.setState({showForm : !this.state.showForm});
}

Function in `setState` to the rescue!

submit(){
this.setState(function(prevState, props){
return {showForm: !prevState.showForm}
});
}

--

--

--

Editor of http://newsletter.fullstackreact.com, fan of all things React

Love podcasts or audiobooks? Learn on the go with our new app.

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
Sophia Shoemaker

Sophia Shoemaker

Editor of http://newsletter.fullstackreact.com, fan of all things React

More from Medium

Redux With Typescript Example

Using the Spotify API with React

Configuring Typescript: Basics

How to use React Router Dom v6