package
Publishing createReducer from https://redux.js.org/recipes/reducing-boilerplate#generating-reducers
npm install redux-create-reducerGitHub-flavored package documentation.
Across all versions, last 30 days.
57,910 in 30 days
Runtime metadata.
Tagged topics.
Estimated unpacked size across 1 packages.
9.2 kB
Includes dependencies.
This code packaged as a node module
Usage:
import { createReducer } from 'redux-create-reducer';
import * as ActionTypes from '../constants/ActionTypes';
const initialState = [];
export const todos = createReducer(initialState, {
[ActionTypes.ADD_TODO](state, action) {
const text = action.text.trim();
return [...state, text];
},
[ActionTypes.REMOVE_TODO](state, action) {
return state.filter((_, i) => i !== action.index);
}
// All other action types result in state being returned
})This library also provides powerful typescript typings when using Action classes:
interface Action {
type: string;
}
interface State {
value: number;
}
class Reset implements Action {
readonly type = 'Reset Action';
}
class AddOne implements Action {
readonly type = 'AddOne Action';
}
class AddCustom implements Action {
readonly type = 'AddCustom Action';
constructor(public readonly value: number) { }
Removing 'Reset Action': (state, action) => ({ value: 0 }), in the reducer causes the error: Property '"Reset Action"' is missing in type .... Similarly, adding 'Nonexisting Action': (state, action) => ({ value: 0 }), causes the type error: Object literal may only specify known properties, and ''Nonexisting Action'' does not exist in type 'Handlers<State, Actions>'.ts(2345)
gajus
gajus@gajus.com