redux-actions
Search…
createAction(s)

Methods

createAction

1
createAction(
2
type,
3
payloadCreator = Identity,
4
?metaCreator
5
)
Copied!
Wraps an action creator so that its return value is the payload of a Flux Standard Action.
1
import { createAction } from 'redux-actions';
Copied!
NOTE: The more correct name for this function is probably createActionCreator(), but that seems a bit redundant.

createAction(type)

Calling createAction with a type will return an action creator for dispatching actions. type must implement toString and is the only required parameter for createAction.
EXAMPLE
1
export const increment = createAction('INCREMENT');
2
export const decrement = createAction('DECREMENT');
3
4
increment(); // { type: 'INCREMENT' }
5
decrement(); // { type: 'DECREMENT' }
6
increment(10); // { type: 'INCREMENT', payload: 10 }
7
decrement([1, 42]); // { type: 'DECREMENT', payload: [1, 42] }
Copied!
If the payload is an instance of an Error object, redux-actions will automatically set action.error to true.
EXAMPLE
1
const noop = createAction('NOOP');
2
3
const error = new TypeError('not a number');
4
expect(noop(error)).to.deep.equal({
5
type: 'NOOP',
6
payload: error,
7
error: true
8
});
Copied!
createAction also returns its type when used as type in handleAction or handleActions.
EXAMPLE
1
const noop = createAction('INCREMENT');
2
3
// As parameter in handleAction:
4
handleAction(noop, {
5
next(state, action) {...},
6
throw(state, action) {...}
7
});
8
9
// As object key in handleActions:
10
const reducer = handleActions({
11
[noop]: (state, action) => ({
12
counter: state.counter + action.payload
13
})
14
}, { counter: 0 });
Copied!
Use the identity form to create one-off actions.
EXAMPLE
1
createAction('ADD_TODO')('Use Redux');
Copied!

createAction(type, payloadCreator)

payloadCreator must be a function, undefined, or null. If payloadCreator is undefined or null, the identity function is used.
NOTE: If payload is an instance of an Error object, payloadCreator will not be called.
EXAMPLE
1
let noop = createAction('NOOP', amount => amount);
2
// same as
3
noop = createAction('NOOP');
4
5
expect(noop(42)).to.deep.equal({
6
type: 'NOOP',
7
payload: 42
8
});
Copied!

createAction(type, payloadCreator, metaCreator)

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.
EXAMPLE
1
const updateAdminUser = createAction(
2
'UPDATE_ADMIN_USER',
3
updates => updates,
4
() => ({ admin: true })
5
);
6
7
updateAdminUser({ name: 'Foo' });
8
// {
9
// type: 'UPDATE_ADMIN_USER',
10
// payload: { name: 'Foo' },
11
// meta: { admin: true },
12
// }
Copied!

createActions

1
createActions(
2
actionMap,
3
?...identityActions,
4
?options
5
)
Copied!
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.
1
import { createActions } from 'redux-actions';
Copied!

createActions(actionMap[, options])

actionMap is an object which can optionally have 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 and meta functions in that order, as in createAction
    • meta is required in this case (otherwise use the function form above)
  • an actionMap
EXAMPLE
1
createActions({
2
ADD_TODO: todo => ({ todo }), // payload creator
3
REMOVE_TODO: [
4
todo => ({ todo }), // payload creator
5
(todo, warn) => ({ todo, warn }) // meta
6
]
7
});
Copied!
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:
EXAMPLE
1
const actionCreators = createActions({
2
APP: {
3
COUNTER: {
4
INCREMENT: [amount => ({ amount }), amount => ({ key: 'value', amount })],
5
DECREMENT: amount => ({ amount: -amount }),
6
SET: undefined // given undefined, the identity function will be used
7
},
8
NOTIFY: [
9
(username, message) => ({ message: `${username}: ${message}` }),
10
(username, message) => ({ username, message })
11
]
12
}
13
});
14
15
expect(actionCreators.app.counter.increment(1)).to.deep.equal({
16
type: 'APP/COUNTER/INCREMENT',
17
payload: { amount: 1 },
18
meta: { key: 'value', amount: 1 }
19
});
20
expect(actionCreators.app.counter.decrement(1)).to.deep.equal({
21
type: 'APP/COUNTER/DECREMENT',
22
payload: { amount: -1 }
23
});
24
expect(actionCreators.app.counter.set(100)).to.deep.equal({
25
type: 'APP/COUNTER/SET',
26
payload: 100
27
});
28
expect(
29
actionCreators.app.notify('yangmillstheory', 'Hello World')
30
).to.deep.equal({
31
type: 'APP/NOTIFY',
32
payload: { message: 'yangmillstheory: Hello World' },
33
meta: { username: 'yangmillstheory', message: 'Hello World' }
34
});
Copied!

createActions(actionMap, ...identityActions[, options])

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(
2
{
3
// function form; payload creator defined inline
4
ACTION_ONE: (key, value) => ({ [key]: value }),
5
6
// array form
7
ACTION_TWO: [
8
first => [first], // payload
9
(first, second) => ({ second }) // meta
10
]
11
12
// trailing action type string form; payload creator is the identity
13
},
14
'ACTION_THREE'
15
);
16
17
expect(actionOne('key', 1)).to.deep.equal({
18
type: 'ACTION_ONE',
19
payload: { key: 1 }
20
});
21
22
expect(actionTwo('first', 'second')).to.deep.equal({
23
type: 'ACTION_TWO',
24
payload: ['first'],
25
meta: { second: 'second' }
26
});
27
28
expect(actionThree(3)).to.deep.equal({
29
type: 'ACTION_THREE',
30
payload: 3
31
});
Copied!

createActions(actionMap[, ...identityActions], options)

You can prefix each action type by passing a configuration object as the last argument of createActions.
EXAMPLE
1
createActions({ ... }, 'INCREMENT', {
2
prefix: 'counter', // String used to prefix each type
3
namespace: '--' // Separator between prefix and type. Default: `/`
4
})
Copied!
'INCREMENT' in this example will be prefixed as counter--INCREMENT.
Last modified 2yr ago