You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Why is this beneficial? **It is often claimed that constants are unnecessary, and for small projects, this might be correct.** For larger projects, there are some benefits to defining action types as constants:
30
30
31
-
* It helps maintain the naming consistent because all action types are gathered in a single place.
32
-
* Sometimes you want to see all existing actions before working on a new feature. It happens that the action you needed was already added by somebody on the team, but you didn’t know.
31
+
* It helps keep the naming consistent because all action types are gathered in a single place.
32
+
* Sometimes you want to see all existing actions before working on a new feature. It may be that the action you need was already added by somebody on the team, but you didn’t know.
33
33
* The list of action types that were added, removed, and changed in a Pull Request, helps everyone on the team keep track of scope and implementation of new features.
34
34
* If you make a typo when importing an action constant, you will get `undefined`. This is much easier to notice than a typo when you wonder why nothing happens when the action is dispatched.
See [redux-action-utils](https://github.com/insin/redux-action-utils) and [redux-actions](https://github.com/acdlite/redux-actions) for examples of such utilities.
142
142
143
143
Note that such utilities add magic to your code.
144
-
Are magic and indirection really worth extra few lines?
144
+
Are magic and indirection really worth it to avoid a few extra few lines of code?
145
145
146
146
## Async Action Creators
147
147
148
-
[Middleware](../Glossary.html#middleware) lets you inject a custom logic that interprets every action object before it is dispatched. Async actions are the most common use case for middleware.
148
+
[Middleware](../Glossary.html#middleware) lets you inject custom logic that interprets every action object before it is dispatched. Async actions are the most common use case for middleware.
149
149
150
150
Without any middleware, [`dispatch`](../api/Store.md#dispatch) only accepts a plain object, so we have to perform AJAX calls inside our components:
However, this quickly gets repetitive because different components request data from the same API endpoints. Moreover, we want to reuse some of this logic (e.g. early exit when there is cached data available) from many components.
235
+
However, this quickly gets repetitive because different components request data from the same API endpoints. Moreover, we want to reuse some of this logic (e.g., early exit when there is cached data available) from many components.
236
236
237
237
**Middleware lets us write more expressive, potentially async action creators.** It lets us dispatch something other than plain objects, and interprets the values. For example, middleware can “catch” dispatched Promises and turn them into a pair of request and success/failure actions.
238
238
@@ -470,7 +470,7 @@ It’s unfortunate that many still choose Flux framework based on whether it use
470
470
471
471
### Generating Reducers
472
472
473
-
Let’s write a function that lets us express reducers as object mapping from action types to handlers. For example, if we want our `todos` reducers to be defined like this:
473
+
Let’s write a function that lets us express reducers as an object mapping from action types to handlers. For example, if we want our `todos` reducers to be defined like this:
474
474
475
475
```js
476
476
exportconsttodos=createReducer([], {
@@ -495,6 +495,6 @@ function createReducer(initialState, handlers) {
495
495
}
496
496
```
497
497
498
-
This wasn’t difficult, was it? Redux doesn’t provide such helper by default because there are many ways to write it. Maybe you want it to automatically convert plain JS objects to Immutable objects to hydrate the server state. Maybe you want to merge the returned state with the current state. There may be different approaches to “catch all” handler. All of this depends on the conventions you choose for your team on a specific project.
498
+
This wasn’t difficult, was it? Redux doesn’t provide such a helper function by default because there are many ways to write it. Maybe you want it to automatically convert plain JS objects to Immutable objects to hydrate the server state. Maybe you want to merge the returned state with the current state. There may be different approaches to a “catch all” handler. All of this depends on the conventions you choose for your team on a specific project.
499
499
500
500
Redux reducer API is `(state, action) => state`, but how you create those reducers is up to you.
0 commit comments