2

I have a multi-page user registration form in my react/redux app.

When a user clicks "next page," it dispatches a call to update the user's profile in the local redux store (not the server).

When they click "Submit" on the final page, I currently have a click handler along the lines of:

clickHandler: function(formData) {

  dispatch(updateProfileInStore(formData);

  dispatch(saveProfileToServer());
}

My question is: can I be 100% certain that the Redux store w/ my user profile will update before the second dispatch is called?

I'm worried that in some cases the profile might be written to my database before the last page of form data is added to it.

2 Answers 2

2

It's not guaranteed. What you could do is pass saveProfileToServer the new form data have it update state and send the updated form data to the server.

As per the react documentation

setState() does not immediately mutate this.state but creates a pending state transition. ... There is no guarantee of synchronous operation of calls to setState and calls may be batched for performance gains.

Sign up to request clarification or add additional context in comments.

3 Comments

A statement like this should be accompanied by references
Good advice Juan Mendes.
The OP is asking about redux not react... You might want to double check what the question is asking.
1

The only way to add a synchronous behaviour here is to either use redux thunk or sagas. A redux thunk returns promise that resolves and in sagas, we may have to watch for success action dispatched by updateProfileInStore which needs to call saveProfileToServer.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.