The ability to reply to discussions is limited to PRO members. Want to join in the discussion? Click here to subscribe now.

Adding a Second Reducer to the Store

Adding a Second Reducer to the Store

2:33
Stores can accept as many reducers as you need. This lesson covers adding a second reducer that will be used to control an array of people.
Watch this lesson now
Avatar
egghead.io

Stores can accept as many reducers as you need. This lesson covers adding a second reducer that will be used to control an array of people.

Avatar
Nicholas

Just to clarify. Sorry but I haven't had the chance to set up your code or I could validate this myself. When the merged observable fires the store.dispatch then the current state is run through both reducers. Is that correct?

Avatar
Nathan Brenner

There's some differences since Angular 2 has added on the module system.

In app.module.ts, import everything as shown and defaultPeople (you'll need to set that as an export in reducers.ts.

Then in @ngModule.imports, you'll need this for it to work with multiple reducers:

StoreModule.provideStore({clock: clock, people: people}, {clock: new Date(), people: defaultPeople})

The first argument of provide store is an object taking the names of the reducers with their value, which you've imported. The second argument is the default state, which even though you've already set that in the reducer function, you need to specify it here as well.

Avatar
doug

Hey,

I was curious how one would solve for loading the default set of people defaultPeople from an api?

I saw some people using the ngrx/effects module and was wondering if this is the right approach or is there a better way?

HEY, QUICK QUESTION!
Joel's Head
Why are we asking?