⚠️ This lesson is retired and might contain outdated information.

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

Andy Van Slaars
InstructorAndy Van Slaars
Share this video with your friends

Social Share Links

Send Tweet
Published 6 years ago
Updated a year ago

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.

Instructor: [00:00] 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.

[00:17] 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.

[00:31] 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.

[00:47] 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.

[00:58] 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.

[01:22] 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.

[01:49] 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.

[02:12] 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.

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

egghead
egghead
~ 15 minutes ago

Member comments are a way for members to communicate, interact, and ask questions about a lesson.

The instructor or someone from the community might respond to your question Here are a few basic guidelines to commenting on egghead.io

Be on-Topic

Comments are for discussing a lesson. If you're having a general issue with the website functionality, please contact us at support@egghead.io.

Avoid meta-discussion

  • This was great!
  • This was horrible!
  • I didn't like this because it didn't match my skill level.
  • +1 It will likely be deleted as spam.

Code Problems?

Should be accompanied by code! Codesandbox or Stackblitz provide a way to share code and discuss it in context

Details and Context

Vague question? Vague answer. Any details and context you can provide will lure more interesting answers!

Markdown supported.
Become a member to join the discussionEnroll Today