ReactJs - How to update state during promise

I am working on an application where, before sending data to the server, we check if the fields are empty and fill them with dummy data.

Status before sending data to the server:

state = {
  title: '',
  body: ''
}

my send function:

this.props.dispatch((dispatch) => {
  dispatch(initializeProcessForm());
  dispatch(processForm(state));
});

Inside initializeProcessFormI check to see if the fields are empty and fill them accordingly, but given that we should not mutate the state , I have to create a new state object and return.

Here I lose the link to the current (new state after the function is completed), and when the send (processForm (state)) is sent to the server, it still stores the old data with empty fields.

How can I get around this problem without changing the way the state responds?

- , API Action , , .

+4
1

, thunks by redux-thunk, - getState

this.props.dispatch((dispatch, getState) => {
  dispatch(initializeProcessForm());
  const updatedState = getState();
  dispatch(processForm(updatedState));
});
+7

Source: https://habr.com/ru/post/1675185/


All Articles