redux-actions
Flux Standard Action utilities for Redux.
Installation
1 | npm install --save redux-actions |
The npm package provides a CommonJS build for use in Node.js, and with bundlers like Webpack and Browserify. It also includes an ES modules build that works well with Rollup and Webpack2‘s tree-shaking.
If you don’t use npm, you may grab the latest UMD build from unpkg (either a development or a production 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.
Usage
createAction(type, payloadCreator = Identity, ?metaCreator)
1 | import { createAction } from 'redux-actions'; |
Wraps an action creator so that its return value is the payload of a Flux Standard Action.
payloadCreator
must be a function, undefined
, or null
. If payloadCreator
is undefined
or null
, the identity function is used.
Example:
1 | let increment = createAction('INCREMENT', amount => amount); |
If the payload is an instance of an Error
object,
redux-actions will automatically set
1 |
|
createAction
also returns its type
when used as type in handleAction
or handleActions
.
Example:
1 | const increment = createAction('INCREMENT'); |
NOTE: The more correct name for this function is probably createActionCreator()
, but that seems a bit redundant.
Use the identity form to create one-off actions:
1 | createAction('ADD_TODO')('Use Redux'); |
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.
createActions(?actionMap, ?...identityActions)
1 | import { createActions } from 'redux-actions'; |
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.
actionMap
is an optional object and a recursive data structure, with action types as keys, and whose values must be either
- a function, which is the payload creator for that action
- an array with
payload
andmeta
functions in that order, as increateAction
meta
is required in this case (otherwise use the function form above)
- an
actionMap
identityActions
is an optional list of positional string arguments that are action type strings; these action types will use the identity payload creator.
1 | const { actionOne, actionTwo, actionThree } = createActions({ |
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:
1 | const actionCreators = createActions({ |
When using this form, you can pass an object with key namespace
as the last positional argument, instead of the default /
.
handleAction(type, reducer | reducerMap = Identity, defaultState)
1 | import { handleAction } from 'redux-actions'; |
Wraps a reducer so that it only handles Flux Standard Actions of a certain type.
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.
Otherwise, you can specify separate reducers for next()
and throw()
using the reducerMap
form. This API is inspired by the ES6 generator interface.
1 | handleAction('FETCH_DATA', { |
If either next()
or throw()
are undefined
or null
, then the identity function is used for that reducer.
If the reducer argument (reducer | reducerMap
) is undefined
, then the identity function is used.
The third parameter defaultState
is required, and is used when undefined
is passed to the reducer.
handleActions(reducerMap, defaultState)
1 | import { handleActions } from 'redux-actions'; |
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.
The second parameter defaultState
is required, and is used when undefined
is passed to the reducer.
(Internally, handleActions()
works by applying multiple reducers in sequence using reduce-reducers.)
Example:
1 | const reducer = handleActions({ |
combineActions(...types)
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.
This allows you to reduce multiple distinct actions with the same reducer.
1 | const { increment, decrement } = createActions({ |
Here’s an example using handleActions
:
1 | const { increment, decrement } = createActions({ |
Usage with middleware
redux-actions is handy all by itself, however, its real power comes when you combine it with middleware.
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 and redux-rx:
1 | const addTodo = createAction('ADD_TODO'); |
See also
Use redux-actions in combination with FSA-compliant libraries.
- redux-promise - Promise middleware
- redux-rx - Includes observable middleware.