Specify reducer and action creators in one swoop. A single function that returns a reducer and action creators. Based on redux-create-module.
npm install --save redux-typescript-module
or yarn add redux-typescript-module
There's just one function: createModule(initalState, handler) -> {reducer, actions}
initialState
is the initial state for the module.
handler
is an object where the keys are action names
and the values are action handlers. For example:
const counter = createModule(0, {
increment: (state: number, action: Action<number>) => state + action.payload,
decrement: (state: number, action: Action<number>) => state - action.payload
})
createModule
returns an object with two things:
actions
is an object with action creators.
for example: counter.actions.increment(5)
will return
{ type: 'increment', payload: 5 }
reducer
is regular reducer that you can pass to the redux store or to combineReducers