Skip to content

Commit 35948cb

Browse files
committed
New docs on action creators
1 parent 3fdce90 commit 35948cb

File tree

1 file changed

+20
-11
lines changed

1 file changed

+20
-11
lines changed

docs/recipes/ReducingBoilerplate.md

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ dispatch({
4949
});
5050
```
5151

52-
you might write an action creator in a separate file, and import it from your component:
52+
You might write an action creator in a separate file, and import it from your component:
5353

5454
#### `actionCreators.js`
5555

@@ -101,9 +101,9 @@ We just modified how `addTodo` action creator behaves, completely invisible to t
101101

102102
### Generating Action Creators
103103

104-
Some frameworks like [Flummox](https://github.com/acdlite/flummox) generate action type constants automatically from the action creator function definitions. The idea is that you don’t need to both define `ADD_TODO` constant and `addTodo()` action creator. Under the hood, such solutions still generate action type constants, but they’re created implicitly so it’s a level of indirection.
104+
Some frameworks like [Flummox](https://github.com/acdlite/flummox) generate action type constants automatically from the action creator function definitions. The idea is that you don’t need to both define `ADD_TODO` constant and `addTodo()` action creator. Under the hood, such solutions still generate action type constants, but they’re created implicitly so it’s a level of indirection and can cause confusion. We recommend creating your action type constants explicitly.
105105

106-
We don’t recommend this approach. If you’re tired of writing simple action creators like:
106+
Writing simple action creators can be tiresome and often ends up generating redundant boilerplate code:
107107

108108
```js
109109
export function addTodo(text) {
@@ -113,6 +113,14 @@ export function addTodo(text) {
113113
};
114114
}
115115

116+
export function editTodo(id, text) {
117+
return {
118+
type: 'EDIT_TODO',
119+
id,
120+
text
121+
};
122+
}
123+
116124
export function removeTodo(id) {
117125
return {
118126
type: 'REMOVE_TODO',
@@ -121,7 +129,7 @@ export function removeTodo(id) {
121129
}
122130
```
123131

124-
you can always write a function that generates an action creator:
132+
You can always write a function that generates an action creator:
125133

126134
```js
127135
function makeActionCreator(type, ...argNames) {
@@ -134,14 +142,15 @@ function makeActionCreator(type, ...argNames) {
134142
}
135143
}
136144

137-
export const addTodo = makeActionCreator('ADD_TODO', 'todo');
138-
export const removeTodo = makeActionCreator('REMOVE_TODO', 'id');
139-
```
140-
141-
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.
145+
const ADD_TODO = 'ADD_TODO';
146+
const EDIT_TODO = 'EDIT_TODO';
147+
const REMOVE_TODO = 'REMOVE_TODO';
142148

143-
Note that such utilities add magic to your code.
144-
Are magic and indirection really worth it to avoid a few extra lines of code?
149+
export const addTodo = makeActionCreator(ADD_TODO, 'todo');
150+
export const editTodo = makeActionCreator(EDIT_TODO, 'id', 'todo');
151+
export const removeTodo = makeActionCreator(REMOVE_TODO, 'id');
152+
```
153+
There are also utility libraries to aid in generating action creators, such as [redux-action-utils](https://github.com/insin/redux-action-utils) and [redux-actions](https://github.com/acdlite/redux-actions). These can help with reducing your boilerplate code and adhereing to standards such as [Flux Standard Action (FSA)](https://github.com/acdlite/flux-standard-action).
145154

146155
## Async Action Creators
147156

0 commit comments

Comments
 (0)