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: README.md
+26-271
Original file line number
Diff line number
Diff line change
@@ -6,258 +6,38 @@
6
6
7
7
[Flux Standard Action](https://github.com/acdlite/flux-standard-action) utilities for Redux.
8
8
9
-
## Installation
10
-
11
-
```bash
12
-
npm install --save redux-actions
13
-
```
14
-
15
-
The [npm](https://www.npmjs.com) package provides a [CommonJS](http://webpack.github.io/docs/commonjs.html) build for use in Node.js, and with bundlers like [Webpack](http://webpack.github.io/) and [Browserify](http://browserify.org/). It also includes an [ES modules](http://jsmodules.io/) build that works well with [Rollup](http://rollupjs.org/) and [Webpack2](https://webpack.js.org)'s tree-shaking.
16
-
17
-
If you don’t use [npm](https://www.npmjs.com), you may grab the latest [UMD](https://unpkg.com/redux-actions@latest/dist) build from [unpkg](https://unpkg.com) (either a [development](https://unpkg.com/redux-actions@latest/dist/redux-actions.js) or a [production](https://unpkg.com/redux-actions@latest/dist/redux-actions.min.js) build). The UMD build exports a global called `window.ReduxActions` if you add it to your page via a `<script>` tag. We *don’t* recommend UMD builds for any serious application, as most of the libraries complementary to Redux are only available on [npm](https://www.npmjs.com/search?q=redux).
redux-actions will automatically set ```action.error``` to true.
47
-
48
-
Example:
49
-
50
-
```js
51
-
constnoop=createAction('NOOP');
52
-
53
-
consterror=newTypeError('not a number');
54
-
expect(noop(error)).to.deep.equal({
55
-
type:'NOOP',
56
-
payload: error,
57
-
error:true
58
-
});
59
-
```
60
-
61
-
`createAction` also returns its `type` when used as type in `handleAction` or `handleActions`.
62
-
63
-
Example:
64
-
65
-
```js
66
-
constnoop=createAction('INCREMENT');
67
-
68
-
// As parameter in handleAction:
69
-
handleAction(noop, {
70
-
next(state, action) {...},
71
-
throw(state, action) {...}
72
-
});
73
-
74
-
// As object key in handleActions:
75
-
constreducer=handleActions({
76
-
[noop]: (state, action) => ({
77
-
counter:state.counter+action.payload
78
-
})
79
-
}, { counter:0 });
80
-
```
81
-
82
-
**NOTE:** The more correct name for this function is probably `createActionCreator()`, but that seems a bit redundant.
83
-
84
-
Use the identity form to create one-off actions:
85
-
86
-
```js
87
-
createAction('ADD_TODO')('Use Redux');
88
-
```
89
-
90
-
`metaCreator` is an optional function that creates metadata for the payload. It receives the same arguments as the payload creator, but its result becomes the meta field of the resulting action. If `metaCreator` is undefined or not a function, the meta field is omitted.
Returns an object mapping action types to action creators. The keys of this object are camel-cased from the keys in `actionMap` and the string literals of `identityActions`; the values are the action creators.
99
-
100
-
`actionMap` is an optional object and a recursive data structure, with action types as keys, and whose values **must** be either
101
-
102
-
- a function, which is the payload creator for that action
103
-
- an array with `payload` and `meta` functions in that order, as in [`createAction`](#createactiontype-payloadcreator--identity-metacreator)
104
-
-`meta` is **required** in this case (otherwise use the function form above)
105
-
- an `actionMap`
106
-
107
-
`identityActions` is an optional list of positional string arguments that are action type strings; these action types will use the identity payload creator.
If `actionMap` has a recursive structure, its leaves are used as payload and meta creators, and the action type for each leaf is the combined path to that leaf:
142
-
143
-
```js
144
-
constactionCreators=createActions({
145
-
APP: {
146
-
COUNTER: {
147
-
INCREMENT: [
148
-
amount=> ({ amount }),
149
-
amount=> ({ key:'value', amount })
150
-
],
151
-
DECREMENT:amount=> ({ amount:-amount }),
152
-
SET:undefined// given undefined, the identity function will be used
Wraps a reducer so that it only handles Flux Standard Actions of a certain type.
16
+
# Getting Started {#gettingstarted}
189
17
190
-
If a `reducer` function is passed, it is used to handle both normal actions and failed actions. (A failed action is analogous to a rejected promise.) You can use this form if you know a certain type of action will never fail, like the increment example above.
191
-
192
-
Otherwise, you can specify separate reducers for `next()` and `throw()` using the `reducerMap` form. This API is inspired by the ES6 generator interface.
18
+
## Installation
193
19
194
-
```js
195
-
handleAction('FETCH_DATA', {
196
-
next(state, action) {...},
197
-
throw(state, action) {...}
198
-
}, defaultState);
20
+
```bash
21
+
$ npm install --save redux-actions
199
22
```
200
23
201
-
If either `next()` or `throw()` are `undefined` or `null`, then the identity function is used for that reducer.
202
-
203
-
If the reducer argument (`reducer | reducerMap`) is `undefined`, then the identity function is used.
204
-
205
-
The third parameter `defaultState` is required, and is used when `undefined` is passed to the reducer.
24
+
or
206
25
207
-
### `handleActions(reducerMap, defaultState, )`
208
-
209
-
```js
210
-
import { handleActions } from'redux-actions';
211
26
```
212
-
213
-
Creates multiple reducers using `handleAction()` and combines them into a single reducer that handles multiple actions. Accepts a map where the keys are passed as the first parameter to `handleAction()` (the action type), and the values are passed as the second parameter (either a reducer or reducer map). The map must not be empty.
214
-
215
-
If `reducerMap` has a recursive structure, its leaves are used as reducers, and the action type for each leaf is the path to that leaf. If a node's only children are `next()` and `throw()`, the node will be treated as a reducer. If the leaf is `undefined` or `null`, the identity function is used as the reducer. Otherwise, the leaf should be the reducer function. When using this form, you can pass an object with key `namespace` as the last positional argument (the default is `/`).
216
-
217
-
The second parameter `defaultState` is required, and is used when `undefined` is passed to the reducer.
218
-
219
-
(Internally, `handleActions()` works by applying multiple reducers in sequence using [reduce-reducers](https://github.com/acdlite/reduce-reducers).)
220
-
221
-
Example:
222
-
223
-
```js
224
-
constreducer=handleActions({
225
-
INCREMENT: (state, action) => ({
226
-
counter:state.counter+action.payload
227
-
}),
228
-
229
-
DECREMENT: (state, action) => ({
230
-
counter:state.counter-action.payload
231
-
})
232
-
}, { counter:0 });
27
+
$ yarn add redux-actions
233
28
```
234
29
235
-
### `combineActions(...types)`
30
+
The [npm](https://www.npmjs.com) package provides a [CommonJS](http://webpack.github.io/docs/commonjs.html) build for use in Node.js, and with bundlers like [Webpack](http://webpack.github.io/) and [Browserify](http://browserify.org/). It also includes an [ES modules](http://jsmodules.io/) build that works well with [Rollup](http://rollupjs.org/) and [Webpack2](https://webpack.js.org)'s tree-shaking.
236
31
237
-
Combine any number of action types or action creators. `types` is a list of positional arguments which can be action type strings, symbols, or action creators.
32
+
The [UMD](https://unpkg.com/redux-actions@latest/dist) build exports a global called `window.ReduxActions` if you add it to your page via a `<script>` tag. We *don’t* recommend UMD builds for any serious application, as most of the libraries complementary to Redux are only available on [npm](https://www.npmjs.com/search?q=redux).
238
33
239
-
This allows you to reduce multiple distinct actions with the same reducer.
redux-actions is handy all by itself, however, its real power comes when you combine it with middleware.
281
-
282
-
The identity form of `createAction` is a great way to create a single action creator that handles multiple payload types. For example, using [redux-promise](https://github.com/acdlite/redux-promise) and [redux-rx](https://github.com/acdlite/redux-rx):
0 commit comments