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.


    Make Dispatching Redux Actions More Reliable With Action Creators

    15 - 16
    3 - 4

    Dispatching actions is how our application sends information to Redux in order to update state. While we could build our action objects inline and use strings for action names, this can be tedious and error prone. In this lesson, we’ll see how the use of constants and action creator functions will make dispatching actions easier and more reliable.



    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




    Here when we call Dispatch, we're creating our action object inline. Since we'll need an action object for any action we want to dispatch, and since it's quite possible that we'll want to dispatch the same type of action for multiple components, let's create a function that will create our action object for us.

    I'm going to take this object that we're passing into Dispatch, and I'm just going to cut it. Then back in my todo.js file, where we're defining the reducer, I'm going to create a new named export.

    I'm going to export const, and I'll call this updateCurrent. updateCurrent is going to be a function that's going to take a value, and that's going to return our object with the type set to currentUpdate and our value passed in as the payload.

    Now that we have this action creator function, we can save this file, and go back into index.js. We'll import that. I'm going to import updateCurrent, and that's going to be imported from my reducers/todo module.

    Then when we call Dispatch here, we're just going to call that with updateCurrent, passing our value through. We can save that. Now we can go in here, and everything should work as expected.

    If we look at our action creator and reducer again, we'll notice that we're using currentUpdate as a string, both in the action creator and in our reducer. If I were to mistype part of my string and save this, when the browser reloads, everything's going to look fine, but as soon as I try to do anything, nothing's going to work, and I can't type into the input.

    The problem here is that I'm dispatching an action, but the type doesn't match up with anything in the reducer, so I just return the state, as is. While using strings here works, it can lead to strange bugs that might be difficult to chase down.

    What I'd rather do is take this string and assign it to a constant that I can do right up here. I can give it the same name as my string, and then I can use this constant in both my action creator and down here in the reducer. Now they're guaranteed to match. If I save this, the browser reloads, and I'm back to being able to type in the input.

    Then if I come back and I make the same mistake that I made earlier, when I save this, I'm going to get an exception. It won't even build my JavaScript.

    Then I can come back in here and very easily track down exactly what the problem is, because it's giving me a line number. Now when it builds, I know that these two things match.

    I'm going to come down here and I'm going to do the same thing with my todo add. I'll declare a constant. We'll call it todo_add.

    We'll just assign the string there, and then we'll replace where I had the string with that constant. Then my code's going to be far less error-prone.