In this post, i'll explain how to pass a state between components in Reactjs.
We'll build a small 'How many books did you read?' app, in this appli...
For further actions, you may consider blocking this person and/or reporting abuse
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 }
})
Why?
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.
Yes this is it
Thank you
Hello Zeyad,
thanks for this state tutorial!
I was asking myself several questions while reading it though. I hope you can enlighten me :)
Why do you keep the book ID in the
<Book>component state ? Wouldn't it be prettier to pass thebook.idprop when you callthis.props.handleCounterinupdateLibraryCount? To me, the ID should not be modified, so we don't have to put it in the state.Is there any particular reason why you don't pass a function as the first argument of this?
this.setState({status: !this.state.status}, this.updateLibraryCount);Thanks again!
See you :)
Thanks Thomas, to your first question, you're right it'll be prettier, you're welcome to contribute and update this on GitHub repo.
To your second question, from Reactjs documentation
and this what i want.
I was talking about the first parameter :)