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
Copy file name to clipboardExpand all lines: docs/recipes/ReducingBoilerplate.md
+20-11Lines changed: 20 additions & 11 deletions
Original file line number
Diff line number
Diff line change
@@ -49,7 +49,7 @@ dispatch({
49
49
});
50
50
```
51
51
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:
53
53
54
54
#### `actionCreators.js`
55
55
@@ -101,9 +101,9 @@ We just modified how `addTodo` action creator behaves, completely invisible to t
101
101
102
102
### Generating Action Creators
103
103
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.
105
105
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:
107
107
108
108
```js
109
109
exportfunctionaddTodo(text) {
@@ -113,6 +113,14 @@ export function addTodo(text) {
113
113
};
114
114
}
115
115
116
+
exportfunctioneditTodo(id, text) {
117
+
return {
118
+
type:'EDIT_TODO',
119
+
id,
120
+
text
121
+
};
122
+
}
123
+
116
124
exportfunctionremoveTodo(id) {
117
125
return {
118
126
type:'REMOVE_TODO',
@@ -121,7 +129,7 @@ export function removeTodo(id) {
121
129
}
122
130
```
123
131
124
-
you can always write a function that generates an action creator:
132
+
You can always write a function that generates an action creator:
125
133
126
134
```js
127
135
functionmakeActionCreator(type, ...argNames) {
@@ -134,14 +142,15 @@ function makeActionCreator(type, ...argNames) {
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
+
constADD_TODO='ADD_TODO';
146
+
constEDIT_TODO='EDIT_TODO';
147
+
constREMOVE_TODO='REMOVE_TODO';
142
148
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?
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).
0 commit comments