Small utility for creating Redux ducks.
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.
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
}
}
*/
Builds a reducer function with defaultState
and actionHandlers
;
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.