Bijou Trouvaille Bijou Trouvaille - 2 years ago 212
Javascript Question

Should mapDispatchToProps dispatch initialization actions?

Suppose a stateless, functional

component that displays user data for the given url. Suppose it is being wrapped with
connect(mapStateToProps, mapDispatchToProps)(UserProfile)
. Finally, suppose a reducer that reduces into
. Anytime the url changes, I need to re-initialize the
, so a solution that comes to mind is to do so from within the mapDispatchToProps like so:

function mapDispatchToProps(dispatch, ownProps) {
return {

Provided that the thunked fetchUser ignores repeated calls by comparing with current state, is this an acceptable practice? Or are there problems associated with calling dispatch immediately from this map function?

Answer Source

This is unsupported and can break at any time.
mapDispatchToProps itself should not have side effects.

If you need to dispatch actions in response to prop changes, you can create a component class and use lifecycle methods for this:

class UserProfile extends Component {
  componentDidMount() {

  componentDidUpdate(prevProps) {
    if ( !== {

  // ...

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download