D
D
DoggoTheKing2019-03-18 20:49:08
JavaScript
DoggoTheKing, 2019-03-18 20:49:08

How to change the execution order of actions in Redux?

There is an action loadUser that receives user data from the REST API using a token (stored in localStorage) and puts it in the global state. It is called in the componentDidMount of the App component, which is the topmost one, the parent of all other components, so to speak.
The action getArtObjects is called in componentDidMount of the Artobjects component. He, using data about the user from the global state, should receive user pictures from the REST API.
But for some reason, when loading the Artobjects component, getArtObjects is called first of the actions, and then loadUser. Because of this, the getArtObjects action cannot get user data from the global state, maybe. they appear later.
5c8fda0d672c4435643085.png
How can I change the order in which actions are called?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Anton Spirin, 2019-03-18
@DoggoTheKing

But for some reason, when loading the Artobjects component, getArtObjects is called first of the actions, and then loadUser. Because of this, the getArtObjects action cannot get user data from the global state, maybe. they appear later.

The componentDidMount method is called after all child components have been mounted, naturally
componentDidMount of the Artobjects component is called before. But even if it were the other way around, it is not certain that the result of the loadUser request would have been returned before the call to getArtObjects. Read about asynchronous programming, you clearly do not fully understand the fundamentals.
Important data for the entire application, such as user data, is better to start loading as early as possible. And this can be done before mounting the root component:
const store = configureStore();

store.dispatch(clientInit());  // тут

const Root = () => (
  <Provider store={store}>
    <Router>
      <App />
    </Router>
  </Provider>
);

And before receiving this data, you can show the preloader and mount Artobjects only after receiving them.
{isInitialDataFetching ? <Preloader /> :  this.renderContent()}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question