0.9.1 • Published 4 years ago
@redux-tools/actions v0.9.1
@redux-tools/actions
An alternative to redux-actions
, implemented using Ramda. Is is essentially a collection of utility functions for creating FSA-compliant action creators and reducers.
Usage
import {
makeActionTypes,
makeConstantActionCreator,
makeSimpleActionCreator,
makeActionCreator,
makeReducer,
} from '@redux-tools/actions';
import { multiply } from 'ramda';
import { alwaysNull } from 'ramda-extension';
export const ActionTypes = makeActionTypes('duck', ['INCREMENT', 'ADD', 'CRAZY_ADD']);
export const increment = makeConstantActionCreator(ActionTypes.INCREMENT);
export const add = makeSimpleActionCreator(ActionTypes.ADD);
export const crazyAdd = makeActionCreator(ActionTypes.CRAZY_ADD, multiply(2), alwaysNull);
export default makeReducer(
[
[ActionTypes.INCREMENT, count => count + 1],
[ActionTypes.ADD, (count, action) => count + action.payload],
[ActionTypes.CRAZY_ADD, (count, action) => count + action.payload / 2],
],
0
);
API Reference
makeActionCreator
Creates a new unary action creator which will apply the provided functions to an argument, producing the payload
and meta
properties.
Arguments
type
(string): The action type.getPayload
(any –> any): Payload getter.getMeta
(any -> any): Meta getter.
Returns
(any -> Action): An action creator.
makeSimpleActionCreator
Creates a new unary action creator which will use the argument as the payload.
Arguments
type
(string): The action type.
Returns
(any -> Action): An action creator.
makeConstantActionCreator
Creates a new nullary action creator.
Arguments
type
(string): The action type.
Returns
(() -> Action): An action creator.
makeReducer
Creates a complex reducer from (type, reducer, errorReducer) tuples, akin to using the switch
statement. Note that you needn't pass the default behavior.
Arguments
tuples
([[string, Reducer, ?Reducer]]): Tuples to combine into a reducer. The third tuple reducer is required only if you plan to pass errors (instanceof Error
) into them, otherwise you can omit it.initialState
(any): Initial state for the reducer.
Returns
(Reducer): A new reducer.