createReducer

node v6.17.1
version: master
endpointsharetweet
const { createStore, combineReducers } = require('redux') const { prop, identity, defaultTo, add, subtract, concat, always } = require('ramda') const [INCREMENT, DECREMENT, PUSH, RESET] = ['INCREMENT', 'DECREMENT', 'PUSH', 'RESET'] const createAction = type => payload => ({ type, payload }) const createReducer = (actions, INITIAL) => (state = INITIAL, { type, payload }) => defaultTo(identity, prop(type, actions))(state, payload) const addNumbers = createAction(PUSH) const decrementCount = createAction(DECREMENT) const count = createReducer({ [INCREMENT]: add, [DECREMENT]: subtract }, 0) const numbers = createReducer({ [PUSH]: concat, [RESET]: always([]) }, []) const { subscribe, dispatch, getState } = createStore(combineReducers({ count, numbers })) subscribe(() => console.log(getState())) dispatch(addNumbers([1, 2, 3])) dispatch(decrementCount(10))
Loading…

no comments

    sign in to comment