Calling SetState() In A Function, And Proceeding To The Next Sibling Function After State Has Changed- Is It Possible?
let say I've got something like this:
In context provider:
toggleLogin(isLoggedIn) {
this.setState({
isLoggedIn: isLoggedIn
});
}
In login component using context:
(....)
this.props.context.toggleLogin(true);
this.props.history.push("/");
(....)
I want this.props.history.push("/") or any else function I'll put after toggleLogin() execute only when toggleLogin function finishes updating state. Right now it's working good.
But I think because setState is async, under the hood it works like:
- execute toggleLogin
- start executing setState in toggleLogin
- execute this.props.history.push("/")
- finish setState in toggleLogin
And it's working in my case because state is set really fast.
But because its working it seems that's it's working this way:
- execute toggleLogin
- execute setState
- execute this.props.history.push("/")
I'm checking isLoggedIn state in "/" route component so thats why I want it to execute only after setState is finished. This is another component so I can't use setState callback.
Is this.props.history.push("/")
really executed after toggleLogin is finished? If not, how can I make it do this?
I don't find making toggleLogin async
and awaiting setState
and then using then()
in login component a good idea - maybe I'm wrong, I'm just starting in ReactJS.
Can anyone enlighten me with right path of doing this? Thanks
Answer
setState
is asynchronous and there is no guarantee that's gonna be finished when you call history.push
. To make sure all updates are done before executing something use the second argument of setState
which allow us to pass a callback
to execute after the state's update. You could pass this callback to toggleLogin
this.props.context.toggleLogin(true, () => this.props.history.push('/'))
And in toggleLogin
toggleLogin(isLoggedIn, cb) {
this.setState({
isLoggedIn: isLoggedIn
}, cb);
}
Related Questions
- → How to update data attribute on Ajax complete
- → October CMS - Radio Button Ajax Click Twice in a Row Causes Content to disappear
- → Octobercms Component Unique id (Twig & Javascript)
- → Passing a JS var from AJAX response to Twig
- → Laravel {!! Form::open() !!} doesn't work within AngularJS
- → DropzoneJS & Laravel - Output form validation errors
- → Import statement and Babel
- → Uncaught TypeError: Cannot read property '__SECRET_DOM_DO_NOT_USE_OR_YOU_WILL_BE_FIRED' of undefined
- → React-router: Passing props to children
- → ListView.DataSource looping data for React Native
- → Can't test submit handler in React component
- → React + Flux - How to avoid global variable
- → Webpack, React & Babel, not rendering DOM