Redux: why do I need to make an action and an Action Cretor?

I am going through the redux section a third time because I am still struggling to wrap my head around it.

My question is about actions and Action Creators. If my action creator function is just going to return an action, why can’t I just use the action object in the first place? Isn’t it reduntant to create a function that returns an object, when I could just use the object in the first place?

for example, could this:

const loginAction = () => {
return {
type: ‘LOGIN’
}
};

store.dispatch(
loginAction()
)

be replaced with this?

const store = Redux.createStore(
(state = {login: false}) => state
);

const loginAction = {
type: login
}

store.dispatch(
loginAction
)

Essentially I’m replacing a redux action creating function with an action object.

I’m also learning, but I see some reasons to do with action creators:
(a) Pass dynamic data to your actions - without it, you only use hardcoded information
(b) Keep the logic outside the reducers - if you need to manipulate data, do it in the actions creator
© Deal with asynchronous actions - if you need to do a server request then that is the best way to deal with (in that case you need a middleware like redux-thunk).

1 Like

You don’t really need an action creator function here, no. They come in handy later when you have actions that need to take parameters to build their payload, as well as with higher-level toolkits that build on your action creator functions. A lot of the React ecosystem is built on higher order functions like this.

1 Like