Join egghead, unlock knowledge.

Want more egghead?

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

Unlock This Lesson
Become a member
to unlock all features

Level Up!

Access all courses & lessons on egghead today and lock-in your price for life.


    Expose Angular Component Logic Using State Reducers

    5 - 6

    A component author has no way of knowing which state changes a consumer will want to override, but state reducers handle this problem by allowing the parent component to override any state change.



    Become a Member to view code

    You must be a Pro 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
    orLog In




    Instructor: This ng template allows the parent component to control what view is displayed based on the state of the toggle component. This toggle component still has some hidden logic that controls how the state is updated, when this toggle function is called.

    In this case, that logic is just toggling the ON value of the component. We're going to use the State Reducer Pattern to give the parent control over how that state is updated.

    First, we'll create an interface for the toggle component state. It just has one Boolean ON property. Then, we'll create a type for the toggle state reducer. It's a function that takes the current state which is of type toggle state and it changes object which is a partial value of that toggle state. That will return a new toggle state.

    This may look familiar to those use Redux, state is your current state, and changes fills the role of the action and it returns a new toggle state. Now, we'll configure our toggle component to take a state reducer as an input that will be of type toggle state reducer.

    We'll initialize that to a function that takes a state and some changes. It returns a new object with that spreads out the current state and overrides that with the new changes that are passed in.

    Now, we need to actually use this state reducer. In this set ON state function, we'll create our old state which is just the current ON state. We'll calculate the new state by using the state reducer function with the old state passed in and the new ON value as it changes object.

    If our state reducer has actually changed the state object, then we will apply the changes with the new state value. Now in order to use this, we'll go to our app component. We'll set the state reducer to our own custom state reducer.

    Now, let's define that in our TypeScript file. Let's say our app component wants to keep track of the number of times the toggle is clicked. We want to create a state reducer that will stop the button from toggling, if we reach a certain limit.

    We'll set our state reducer function to be a function that takes the current state and some changes. If the number of times clicked is more than our limit, we'll just return the existing state with no changes. If the ON value has actually being modified, we're going to increment this.times clicked by one.

    Finally, we'll pass through our changes just like the default state reducer. Now, let's see if this works. We're going to keep clicking and it stopped. Looks like it's working.