DEV Community

Discussion on: How to pass state between components in reactjs

Collapse
 
uranium93 profile image
uranium93

salem
nice explanation but it's not recommended to use :
this.setState({status: !this.state.status}, this.updateLibraryCount);
|.|
|_|
V
this.setState({status: !this.state.status} you better use

this.setState((prevState) => {
return { status: !prevState.status }
})

Collapse
 
zeyadetman profile image
Zeyad Etman

Why?

Collapse
 
chiangs profile image
Stephen Chiang

setState happens asynchronously... Therefore you need to use previous state first to ensure you are using the latest if you are going to calculate the state based on previous state.

Thread Thread
 
uranium93 profile image
uranium93

Yes this is it
Thank you