Redux FAQ: Actions
- Why should type be a string, or at least serializable? Why should my action types be constants?
- How can I represent “side effects” such as AJAX calls? Why do we need things like “action creators”, “thunks”, and “middleware” to do async behavior?
- Should I dispatch multiple actions in a row from one action creator?
Actions
We can't reliably enforce serializable actions for performance reasons, so Redux only checks that every action is a plain object, and that the type
is defined. The rest is up to you, but you might find that keeping everything serializable helps debug and reproduce issues.
Encapsulating and centralizing commonly used pieces of code is a key concept in programming. While it is certainly possible to manually create action objects everywhere, and write each value by hand, defining reusable constants makes maintaining code easier. If you put constants in a separate file, you can check your import
statements against typos so you can't accidentally use the wrong string.
Further information
Documentation
Reducing Boilerplate
Discussion- #628: Solution for simple action creation with less boilerplate
- #1167: Reducer without switch
No. We suggest you write independent small reducer functions that are each responsible for updates to a specific slice of state. We call this pattern “reducer composition”. A given action could be handled by all, some, or none of them. This keeps components decoupled from the actual data changes, as one action may affect different parts of the state tree, and there is no need for the component to be aware of this. Some users do choose to bind them more tightly together, such as the “ducks” file structure, but there is definitely no one-to-one mapping by default, and you should break out of such a paradigm any time you feel you want to handle an action in many reducers.
Further information
Documentation
Recipes: Structuring Reducers
Discussions- #1167: Reducer without switch
- Stack Overflow: Can I dispatch multiple actions without Redux Thunk middleware?
This is a long and complex topic, with a wide variety of opinions on how code should be organized and what approaches should be used.
Redux is inspired by functional programming, and out of the box, has no place for side effects to be executed. In particular, reducer functions must always be pure functions of (state, action) => newState
. However, Redux's middleware makes it possible to intercept dispatched actions and add additional complex behavior around them, including side effects.
In general, Redux suggests that code with side effects should be part of the action creation process. While that logic can be performed inside of a UI component, it generally makes sense to extract that logic into a reusable function so that the same logic can be called from multiple places—in other words, an action creator function.
The simplest and most common way to do this is to add the middleware that lets you write action creators with more complex and asynchronous logic. Another widely-used method is Redux Saga which lets you write more synchronous-looking code using generators, and can act like “background threads” or “daemons” in a Redux app. Yet another approach is , which inverts the process by allowing your reducers to declare side effects in response to state changes and have them executed separately. Beyond that, there are many other community-developed libraries and ideas, each with their own take on how side effects should be managed.
Further information
Documentation
- Advanced: Async Flow
Articles- From Flux to Redux: Async Actions the easy way
Gist: Redux-Thunk examples
Discussions- #455: Modeling side effects
- #569: Proposal: API for explicit side effects
- Stack Overflow: Why do we need middleware for async flow in Redux?
- Stack Overflow: Where should I put synchronous side effects linked to actions in redux?
- Stack Overflow: How to fire AJAX calls in response to the state changes with Redux?
- Twitter: possible comparison between sagas, loops, and other approaches
There are , but the most commonly used ones are redux-thunk
, , and redux-observable
. These are different tools, with different strengths, weaknesses, and use cases.
As a general rule of thumb:
- Thunks are best for complex synchronous logic (especially code that needs access to the entire Redux store state), and simple async logic (like basic AJAX calls). With the use of
async/await
, it can be reasonable to use thunks for some more complex promise-based logic as well. - Sagas are best for complex async logic and decoupled "background thread"-type behavior, especially if you need to listen to dispatched actions (which is something that can't be done with thunks). They require familiarity with ES6 generator functions and
redux-saga
's "effects" operators. - Observables solve the same problems as sagas, but rely on RxJS to implement async behavior. They require familiarity with the RxJS API.
We recommend that most Redux users should start with thunks, and then add an additional side effect library like sagas or observables later if their app really requires handling for more complex async logic.
Articles
- Decembersoft: Redux-Thunk vs Redux-Saga
Redux-Saga V.S. Redux-Observable
Discussions- Stack Overflow: Pros/cons of using redux-saga with ES6 generators vs redux-thunk with ES2017 async/await
There's no specific rule for how you should structure your actions. Using an async middleware like Redux Thunk certainly enables scenarios such as dispatching multiple distinct but related actions in a row, dispatching actions to represent progression of an AJAX request, dispatching actions conditionally based on state, or even dispatching an action and checking the updated state immediately afterwards.
In general, ask if these actions are related but independent, or should actually be represented as one action. Do what makes sense for your own situation but try to balance the readability of reducers with readability of the action log. For example, an action that includes the whole new state tree would make your reducer a one-liner, but the downside is now you have no history of why the changes are happening, so debugging gets really difficult. On the other hand, if you emit actions in a loop to keep them granular, it's a sign that you might want to introduce a new action type that is handled in a different way.
Try to avoid dispatching several times synchronously in a row in the places where you're concerned about performance. There are a number of addons and approaches that can batch up dispatches as well.
Further information
Documentation