Search code examples
reactjsreduxreact-reduxactiondispatch

Can I have a dispatch (type and payload) out of an action at react redux ? Or it's a bad practice?


I already now that this code works, but I'm doubt if it is a bad practice.

Can I have a dispatch passing type and payload out of an actions ?

await store.dispatch({
  type: MY_TYPE,
  payload: myPayload
})

Or this is a bad practice ? (why)

(my guess) Only actions can dispatch data using type and payload (?)


Solution

  • According to the Redux docs here: https://redux.js.org/recipes/reducing-boilerplate#action-creators

    Action creators have often been criticized as boilerplate. Well, you don't have to write them! You can use object literals if you feel this better suits your project.

    It seems totally fine; but if you keep reading, they explain the benefits of using action creators as opposed to object literals:

    Action creators let you decouple additional logic around dispatching an action, from the actual components emitting those actions.