actionz
v1.0.1
Published
Utility module for creating redux ducks.
Downloads
4
Maintainers
Readme
Actionz
Small utility for creating Redux ducks.
API
createAction(type: string, payloadCreator: Function)
Builds an actor creator funciton with type
and a payloadCreator
that will receive all arguments passed to the action creator and returns a payload for the action. If no payloadCreator
is provided, the first argument to the action creator will be used as the payload.
Example
const increment = createAction(INCREMENT);
console.log(increment());
/*
{
type: 'INCREMENT'
}
*/
const incrementBy = createAction(INCREMENT_BY);
console.log(incrementBy(2));
/*
{
type: 'INCREMENT_BY',
payload: 2
}
*/
const incrementThenMultiply = createAction(INCREMENT_THEN_MULTIPLY,
(inc: number, mult: number) => ({ inc, mult }));
console.log(incrementThenMultiply(2, 3));
/*
{
type: 'INCREMENT_THEN_MULTIPLY',
payload: {
inc: 2,
mult: 3
}
}
*/
createReducer(defaultState: Object, actionHandlers: Object)
Builds a reducer function with defaultState
and actionHandlers
;
Example
const reducer = createReducer(defaultState, {
[INCREMENT]: (payload, { count }) => ({ count: count + 1 }),
[INCREMENT_BY]: (inc, { count }) => ({ count: count + inc }),
[DECREMENT]: (payload, { count }) => ({ count: count - 1 }),
[INCREMENT_THEN_MULTIPLY]: ({ inc, mult }, { count }) => ({ count: (count + inc) * multi }),
});
Check the examples folder for full examples.