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.

Add `state` as the third parameter to `mapDispatchToProps`

See original GitHub issue

So that I could do this:

var ToggleFollowButton = ({toggleFollow}) => <button onClick={toggleFollow}>Toggle</button>;

ToggleFollowButton = connect(null, (dispatch, ownProps, state) => {

  const toggle = state.postsFollowing[ownProps.id]
    ? unfollowPostActionCreator 
    : followPostActionCreator;

  return {
    toggleFollow: () => dispatch(toggle(ownProps.id)))
  };

})(ToggleFollowButton)

Now I’m fully aware that I can do this in the component code by passing the “follow state” and both action creators to it, but when using function components this would be more performant way to do this. In normal class based component I can do this only once as a method but with a function component I lose some performance because the toggle function is recreated on every render. Also when using react/jsx-no-bind eslint rule it’s hard to avoid the warning.

Another point is that if the toggle function is recreated on every render it can interfere with PureRenderMixin optimized components further down in the component tree.


Another option would be to add dispatch to mapStateToProps as the third argument. Which actually could be prettier because then the object shorthand for mapDispatchToProps would be still available.

EDIT: Also it would be easier to implement because the reinvoke semantics of mapDispatchToProps would not change. So I’ll actually propose this version 😃

I can also send a PR if we see this as something we want.

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:18 (10 by maintainers)

github_iconTop GitHub Comments

156reactions
gaearoncommented, Jan 4, 2016

Same example in a less terse way:

function mapStateToProps(state, ownProps) {
  return {
    isFollowing: state.postsFollowing[ownProps.id]
  };
}

function mergeProps(stateProps, dispatchProps, ownProps) {
  const { isFollowing } = stateProps;
  const { dispatch } = dispatchProps;
  const { id } = ownProps;

  const toggle = isFollowing ?
    unfollowPostActionCreator :
    followPostActionCreator;

  return {
    ...ownProps,
    toggleFollow: () => dispatch(toggle(id)))
  };
}

ToggleFollowButton = connect(
  mapStateToProps,
  null,
  mergeProps
})(ToggleFollowButton)
32reactions
gaearoncommented, Jan 4, 2016

connect() accepts a third argument called mergeProps precisely for this use case.

var ToggleFollowButton = ({toggleFollow}) => <button onClick={toggleFollow}>Toggle</button>;

ToggleFollowButton = connect(
  ({ postsFollowing }, { id }) => ({
    isFollowing: postsFollowing[id]
  }), (dispatch) => ({
    dispatch
  }), ({ isFollowing }, { dispatch }, { id, ...otherProps }) => ({
    ...otherProps,
    toggleFollow: isFollowing ?
      () => dispatch(unfollowPostActionCreator(id))) :
      () => dispatch(followPostActionCreator(id)))      
  })
})(ToggleFollowButton)

Or, in a more readable way:

I think adding getState() to mapDispatchToProps has the potential of being very confusing and people might start using it instead of mapStateToProps and wonder why it doesn’t redraw.

Read more comments on GitHub >

github_iconTop Results From Across the Web

React-Redux: Add custom third parameter to mapStateToProps
I have an application using React and Redux. Several parts of my data model are potientially lazy, so a common pattern when reading...
Read more >
Connect | React Redux
mergeProps should be specified with maximum of three parameters. They are the result of mapStateToProps() , mapDispatchToProps() , and the ...
Read more >
The Ultimate Manager: Redux III mapStateToProps + ...
mapDispatchToProps () takes an argument of "dispatch". Dispatch is a function of the Redux store and it is the only way to trigger...
Read more >
Redux connect in depth. - ВКонтакте
The mapStateToProps is a function with two params. The first param is state which is the redux state, where you get your redux...
Read more >
Defining and Using React Redux mapDispatchToProps
Providing a mapDispatchToProps Parameter ... The second way to use the mapDispatchToProps function to map the dispatch functions to our React ...
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