Notes

Redux Reducers in Depth

Edit on GitHub

Redux

createReducer()

1createReducer(initialState, {
2  // object mapping action types to 'case' reducers
3})
1const counterReducer = createReducer(0, {
2  increment: (state, action) => state + action.payload,
3  decrement: (state, action) => state - action.payload
4})
1// Actions creators generated by createAction
2const increment = createAction('increment')
3const decrement = createAction('decrement')
4
5const counterReducer = createReducer(0, {
6  [increment]: (state, action) => state + action.payload, // reference generated action creator as a computed property
7  [decrement.type]: (state, action) => state - action.payload // Typescript may want [action.type] to force compiler to compute property
8})
  • [increment] = computed property syntax (allows you to put an expression in brackets [])
  • Both [increment] and [increment.type] will work.