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})
Typescript typings
This library also provides powerful typescript typings when using Action classes:
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)