Step 2.8
Combine Reducers
This lesson is just a helper to make the process of writing reducers use less boilerplate code. This step briefly introduces to a world of helpers in writing Redux code.
Take a peek at useful helpers and middleware created by community are:
-
immer: https://github.com/mweststrate/immer - improves ergonomics of working with immutables by introducing the concept of mutating a draft
-
redux-starter-kit: https://github.com/reduxjs/redux-starter-kit - help address common concerns of Redux in boilerplate and complexity
Exercise
-
open up
exercise/src/reducers/index.ts -
implement the
filterReducerfunction with a switch / case statement - it is contrived to have a switch case for ONE condition, but serves to be a good exercise here -
replace the export reducer function with the help of the
combineReducer()function fromredux
Bonus Exercise
The Redux team came up with redux-starter-kit to address a lot of boilerplate concerns. They also embed the immer library to make it nicer to write reducer functions. So, let's try out immer! Look at this example: https://github.com/mweststrate/immer#reducer-example
-
import immer into the
exercise/src/reducers/pureFunction.tsfile -
replace the implementation of the pure functions with the help of immer's
produce() -
run
npm testin the root folder to see if it still works! -
look at the web app to make sure it still works!