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.


    Handle Multiple Actions using a Single Function with combineActions in redux-actions


    Sometimes, multiple actions can be used to update state using the same function. In this lesson, we’ll see how to use combineActions to handle separate but related actions in a single action handler.



    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: If we take a look at the show loader reducer and the hide loader reducer, we'll notice that they are doing exactly the same thing, because our payload creator function for these actions is actually deciding what the payload value should be, and a reducer is essentially just setting the isLoading flag based on that payload value.

    We can use redux-actions to combine these into a single reducer. Let's start by jumping to the top of our file and updating our import to also pull in the combined actions function from redux-actions.

    We can scroll back down to where these reducers are defined. Let's take these two reducers and we'll comment them out for now. Let's create a new reducer in their place. We'll just call it the loader reducer.

    This is going to be a call to handleAction, and handleAction's first argument is the action type. We need to create a new action type for this action that combines our two actions.

    Let's create a loader action. We are going to make that a call to combine actions, and that's going to take show loader and hide loader as its arguments. Now we have this combination action that we can use in place of an individual action in our call to handleAction.

    We'll use handleAction as we did before. Our second argument is going to be a reducer function. That's going to take state and an action, and it's going to return new state. We can just copy that from right here. It's going to take our initial state as an argument.

    We'll drop down to the bottom here. We'll define a single loader reducer instead of passing it to individual reducers. We can save this. We'll jump over to the browser, and we'll verify that everything is still working, and our loader is being shown and hidden as expected.

    Now I can jump back into the code. I can come up here, and I can just remove these...comment it out, reducers. If we wanted to skip the step where we defined this new variable, we could honestly just take combined actions, cut it from there, paste it in as the first argument to handleAction. There's no need to create that intermediate variable.

    I'll save this, just verify that my refactor hasn't broken anything. We're still good to go.