do(callback)
create.do
create(actionType).do
Appropriate leaf state: any
Returns a Riduce-standard action that the reducer uses to non-mutatively update the leaf's state to the return value of callback(leafState, treeState)
.
Note: creating an action using do(callback)
does not follow Redux's non-enforced recommendation that actions should always be serializable, since the resultant action will have the function callback
as its payload
.
callback
(function): invoked by the leaf's reducer with two arguments, leafState
and treeState
action
(object): a Riduce-standard action
import { createStore } from 'redux'
import riduce from 'riduce'
const initialState = {
bool: false,
num: 2,
str: 'foo',
arr: [1, 2, 3]
}
const [reducer, actions] = riduce(initialState)
const store = createStore(reducer)
create.do
on a leaf:const doToString = actions.str.create.do
store.dispatch(doToString(state => state.toUpperCase()))
console.log(store.getState().str) // 'FOO'
create(actionType).do
on a leaf:const doToBoolean = actions.bool.create('APPLY_TO_BOOLEAN').do
store.dispatch(doToBoolean(state => !state))
console.log(store.getState().bool) // true
create.do
on a branch:const doToState = actions.create.do
store.dispatch(doToState(state => ({ num: state.num, arr: state.arr }))
console.log(store.getState()) // { num: 2, arr: [1, 2, 3] }
create.do
with two arguments:const doToArray = actions.arr.create.do
store.dispatch(doToArray(
(leafState, treeState) => leafState.map(element => element * treeState.num)
))
console.log(store.getState()) // { num: 2, arr: [2, 4, 6] }