Redux-Leaves: custom logic
const { createStore } = require('redux')
const reduxLeaves = require('redux-leaves').default
const initialState = {
counter: 2,
list: ['first', 'second'],
nested: { arbitrarily: { deep: 0 } }
}
// Key your reducer logic by a descriptive verb
const reducersDict = {
double: leafState => leafState * 2,
appendToEach: (leafState, action) => leafState.map(str => str.concat(action.payload)),
countTreeKeys: (leafState, action, treeState) => Object.keys(treeState).length
}
// Provide the dictionary of your reducer logic to reduxLeaves
const [reducer, actions] = reduxLeaves(initialState, reducersDict)
const store = createStore(reducer)
store.getState()
// custom double creator
store.dispatch(actions.counter.create.double())
store.getState().counter // 4
// custom appendToEach creator
store.dispatch(actions.list.create.appendToEach(' item')) // ' item' will be the action payload
store.getState().list // ['first item', 'second item']
// custom countTreeKeys creator
store.dispatch(actions.nested.arbitrarily.deep.create.countTreeKeys())
store.getState().nested.arbitrarily.deep // 3
no comments