redux-ramda
Ramda based functional helpers for redux
I like using the cond
function from Ramda to describe a list of functions that can occur based on an array of predicates.
Redux reducers feel like they could have a similar API - so I made this wrapper.
Example
// ACTION TYPESconst SET_FILTER = "SET_FILTER"const ADD_TODO = "ADD_TODO" // ACTION CREATORSconst setFilter = { return type: SET_FILTER payload: filter}const addTodo = { return type: ADD_TODO payload: todo} // REDUCERconst initialState = filter: null todos: const reducer =
API
createReducer(initialState, reducerSpec) => reducer
The reducer spec is a list of [predicate, transform] pairs - similar to cond
: http://ramdajs.com/docs/#cond
Please see the tests for more examples.
There is some added sugar around the spec:
- If the predicate isn't a function it will be wrapped in
propEq("type")
, this allows action types to be passed in directly - Transform functions are curried and receive the action payload, followed by the current state. They need to return the next state
- A pass through condition is added automatically
- Multiple transform functions can be added to a single predicate - useful for re-using transforms, e.g. a RESET action may have the same effect as a RESET_FILTER and RESET_SORT actions combined. With named functions this becomes nice and declarative. For example: