loading...

What's the difference between an action creator and a action?

mcrowder65 profile image Matt Crowder ・1 min read

An action is simply an object that has two things: a type, and a payload.

An action creator is simply a function, that just returns an action.

Why do we care about action creators? I don't think one should care to correct people who don't differentiate between actions and action creators, but it is a good thing to make action creators, because it provides a way to have reusable actions that you can use all throughout your code base.

Also, you can add logic in there that you may not want to have to remember to do each time you use an action.

My favorite example is when you have to create a uniquely generated id.

import shortid from "shortid";

export const addPost = ({ title, body }) => {
    return {
        title,
        body,
        id: shortid.generate()
    };
};

What do you like to do with your action creators?

Posted on by:

mcrowder65 profile

Matt Crowder

@mcrowder65

Software Engineer at Appian, previously at Walmart Labs. Co-organizer of novajavascript.com, public speaker, teacher, codementor.io/mcrowder65

Discussion

markdown guide