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 Actions in Redux with mapDispatchToProps

    15 - 16
    3 - 4

    We need to get our dispatch functions into our components in order to respond to events in Redux. Connect’s second argument is a function called mapDispatchToProps and it does exactly what the name implies. In this lesson, we’ll use this function to get our dispatch functions into our components via props and refactor to use these functions accordingly.



    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




    Using the provider component and the connect function we've wired up the state from our Redux store as props in our connected app component. In our entry file, we're still manually passing in our dispatch function. Let's refactor this code and handle our dispatch functions like we're handling our state.

    I'm going to start by moving the imports for the update current function from our reducer and bind action creators from Redux. I'm going to cut those out of my entry file and I'm going to move into add.js, and add those to my imports there.

    Now back in index.js I'm going to take this actions that I'm creating by calling bind action creators and I'm going to cut that and I'm also going to move that over into add.js. I'm going to scroll down it here and put that under my component definition and then back in index.js. I'm just going to update this reference to the app component and remove that passed in prop.

    I can save index.js and then back in app. Let's finish updating our code. We're using a function called map state to props as the first argument for connect. Connect takes a second argument called map dispatch to props. Let's define that.

    Map dispatch to props is a function that's going to accept the dispatch method from our store. Then it's going to return an object that contains dispatch functions just like bind action creators does.

    I'm going to come up here and I'm take this bind action creators call. I'm going to cut that out, get rid of actions, because we're not going to need that anymore. I'm going to paste that in here, and I'm just going to get rid of the reference to store because dispatch is being passed in as our argument here.

    Now that we've defined map dispatch to props let's pass it into connect as our second argument. With that done we can save this and our browser will reload. What's going to happen is it's not going to work, because changecurrent is not a function.

    In the previous version of our code we were passing this updatecurrentaction function in with the name changecurrent. Because we're using that dispatch to props here and all we're returning is an object with update current as the key and value, that's what the property is going to be named.

    I could add the key that our code is looking for by adding it here, or if I want to keep this nice and short all I have to do is go into my component and update this to use updatecurrent. We can save that and now our component works as expected.