question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Dispatch action or action creator?

See original GitHub issue

A little confused so please excuse my ignorance.

In the readme I see an example _dispatching the action_:

class UserComponent extends React.Component {
  ...
  onSomeButtonClicked() {
    const { userId, dispatch } = this.props
    dispatch({type: 'USER_FETCH_REQUESTED', payload: {userId}})
  }
  ...
}

In an example I see _dispatching of the action creator_?

import { selectReddit, invalidateReddit } from '../actions'

class App extends Component {
  ...
  handleChange(nextReddit) {
    this.props.dispatch(selectReddit(nextReddit))
  }
  ...
}

Any reason for the variation? Don’t both approaches do the same thing?

I thought one of the benefits of sagas was the fact you no longer needed to do as much importing (hence the first example).

Trying to standardize an approach before I convert over to sagas.

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:1
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
slorbercommented, Jun 15, 2016

@DarrylD thanks 😃

I think we allow to dispatch an action creator because people might want to keep using thunks with redux-saga. I think thunks are not needed anymore with redux-saga, but one might want to migrate progressively to sagas and keep both during the migration.

In this example as the selectReddit is not a thunk I don’t see much interest, except the ability to have an action builder, on which you can easily add assertions, and eventually a typesystem like typescript or flow. I’d recommend to keep the usage of actionCreators just for that as it can help maintainability.

Wether you are in a saga, or in a componnt, using an actionCreator has these advantages:

this.props.dispatch(selectReddit(nextReddit))

// or 

yield put(selectReddit(nextReddit))
0reactions
DarrylDcommented, Jun 15, 2016

Interesting. I’m not using thunks at the moment but, I think I’ll play around some some options. I have to say, at this moment, AC’s seems pretty redundant for my use case. Maybe maintainability would be a factor but, it seems like the additional boilerplate/overhead offsets a bit.

Also gonna implement ducks, that should also help a bit.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Action Creators - Human Redux
It's just a utility that takes an action creator (or a whole object of action creators) and binds them all to the dispatch...
Read more >
2.9 Create Action Creators For Each Action - Educative.io
These action creators are passed to the dispatch function as the result value, and the dispatch is executed.
Read more >
Different Ways to Dispatch Actions with Redux - Pluralsight
Let's look at the various ways to dispatch actions with Redux. ... each function inside it is assumed to be a Redux action...
Read more >
Connect: Dispatching Actions with mapDispatchToProps
Providing a mapDispatchToProps allows you to specify which actions your component might need to dispatch. It lets you provide action dispatching ...
Read more >
Back to Basics: What's the Difference between an Action and ...
When writing basic Redux, an action creator simply returns an action. You would typically dispatch the action to your store immediately. store.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found