Reducers
Reducers specify how the feature state changes in response to actions sent to the store. A reducer function typically looks like this:
todo-reducer.ts
#
Register feature reducerBefore we can update state by dispatching actions, we need to add the reducer to the store. There are 2 options to register a feature reducer:
#
Option 1: Store ConfigLike this the reducers are ready when the store is initialized.
#
Option 2: Add feature reducers dynamicallyWe can add feature reducers at any time with store.feature
.
#
Update StateNow we are all set for updating the todos feature state.
Let's dispatch the AddTodo
action:
Yes, we did it! The todoReducer processed the action, and the new todo landed in the todos
array.