@react-nano/tsrux
Version:
tsrux stands for typesafe redux. It reduces boilerplate redux code.
15 lines (14 loc) • 662 B
JavaScript
/**
* Creates a reducer, which is capable of infering the type of action for each reducer based on the action-creators.
*
* @param defaultState The initial state value
* @param setup This function is used to set up the contained reducers.
*/
export function mapReducers(defaultState, setup) {
const map = Object.assign({}, ...setup((actionCreator, reducer) => ({ [actionCreator.type]: reducer })));
// eslint-disable-next-line default-param-last, @typescript-eslint/default-param-last
return (state = defaultState, action) => {
const reducer = map[action.type];
return reducer ? reducer(state, action) : state;
};
}