Join egghead, unlock knowledge.

Want more egghead? It's 45% off for a limited time only!

This lesson is for members. Join us? Get access to all 3,000+ tutorials + a community with expert developers around the world.

Unlock All Content for 45% Off

Already subscribed? Sign In

Save 45% for a limited time.

Get access to all courses and lessons on egghead today.

Autoplay

    Use Two Reducers Together

    John LindquistJohn Lindquist

    Reducers can easily be used inside one another to update related state. This lesson shows how you can use the clock reducer inside of the people reducer to create the time for each new person.

    angularAngular
    2 - 6
    ngrx-store@ngrx/store
    rxjsRxJS
    5 - 6
    Code

    Code

    Become a Member to view code

    You must be a Member to view code

    Access all courses and lessons, track your progress, gain confidence and expertise.

    Become a Member
    and unlock code for this lesson
    Transcript

    Transcript

    00:00 One of the neatest things about reducers like this clock reducer is that I can actually use it in other places. I can just drop it in here, say, "Hey, I want a default time which I can manage inside of my people reducer from this clock." If I try it right now you'll see I'll have an undefined type. You can't read type of undefined.

    00:23 We'll go ahead and set a default type on this, just to save us from hitting that error because it's checking for type and that's undefined on that first pass. Now when it's undefined, it'll get that assigned to an empty string. Hit the empty string and return to state.

    00:42 Now when I hit Save and Refresh, you can see now I'll get the time Sarah is in this and I can do that for each of our people here. I'll copy and paste clock, clock, clock. I'm also going to update this pipe here. We're using Date and the date is formatted just JMS, to give us the hour and seconds.

    01:14 You'll see Sarah, John, Nancy and Drew are all in that same time and that's the time that this timer started. Our timer is still incrementing three seconds at a time. Let's go ahead and change that so it's just doing one second. You can see that it all started in the same spot and now our main clock is incrementing but these guys are all locked in time from where we started.

    Discuss

    Discuss