Repository navigation
Redux Asynchronous Actions
Alistair Blake edited this page May 25, 2019
·
1 revision
https://redux.js.org/advanced/async-actions
-
When you call an asynchronous API, there are two crucial moments in time: the moment you start the call, and the moment when you receive an answer (or a timeout).
-
Each of these two moments usually require a change in the application state; to do that, you need to dispatch normal actions that will be processed by reducers synchronously. Usually, for any API request you'll want to dispatch at least three different kinds of actions:
- The reducers may handle this action by toggling an isFetching flag in the state. This way the UI knows it's time to show a spinner.
- The reducers may handle this action by merging the new data into the state they manage and resetting isFetching. * The UI would hide the spinner, and display the fetched data.
- The reducers may handle this action by resetting isFetching. Additionally, some reducers may want to store the error message so the UI can display it.
- Just like in the basic tutorial, you'll need to design the shape of your application's state before rushing into the implementation. With asynchronous code, there is more state to take care of, so we need to think it through.
- Finally, how do we use the synchronous action creators we defined earlier together with network requests? The standard way to do it with Redux is to use the Redux Thunk middleware. It comes in a separate package called redux-thunk. We'll explain how middleware works in general later; for now, there is just one important thing you need to know: by using this specific middleware, an action creator can return a function instead of an action object. This way, the action creator becomes a thunk.