M
M
Maxim2015-12-04 15:03:39
React
Maxim, 2015-12-04 15:03:39

How to execute an action after another action has been executed?

There are 2 actions. Let's assume action1 and action2. Action1 - asynchronous receiving of data from the server, action2 - selection of one object in the array of received data.
In the life cycle of an application, a situation may arise when action2 is executed before action1. How to make action2 wait for action1 to execute?
I will be grateful for help.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
Nikita Gushchin, 2015-12-04
Antonikhin @STJ

The only way to execute actions natively in redux in a certain order is thunk

functiob action1Creator() {
  return (dispatch, getState) => {
    fetch(...)
      .then(JSON.parse)
      .then((response) => dispatch(action2Creator(response)))
    ;
  };
}

function action2Creator(response) {
  return { type: 'SOME', data: response.data[12] };
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question