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.

Access state props in `mapDispatchToProps`

See original GitHub issue

I was wondering why it is not possible to access state-derived props in mapDispatchToProps. For instance, given the following mapStateToProps function:

const mapStateToProps = (state, props) => {
  return {
    quota: state.quota,
  };
};

I would like to access props.quota in the corresponding mapDispatchToProps:

const mapDispatchToProps = (dispatch, props) => {
  return {
    unlockItem(item_id) {
      if (props.quota > 0) {
        dispatch(unlockItem(item_id));
      }
    },
  };
};

And if it is possible, how. Because I tried and could not. And I could not find anything about it in the documentation.

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:16
  • Comments:10 (3 by maintainers)

github_iconTop GitHub Comments

24reactions
jimbollacommented, Nov 1, 2016

You can’t access state in mDTP. But you can in mergeProps, so your connect would look something like:

connect(
  state => ({ quota: state.quota }),
  { unlockItem }.
  (stateProps, dispatchProps, ownProps) => ({
    ...ownProps,
    ...stateProps,
    ...dispatchProps,
    unlockItem(item_id) {
        if (stateProps.quota > 0) {
          dispatchProps.unlockItem(item_id)
        }
    }
  })
)(YourComponent)

I myself typically use recompose library to do is a slightly different way. I’d do:

compose(
  connect(
    state => ({ quota: state.quota }),
    { unlockItem }
  ),
  withHandlers({
    unlockItem: => props => item_id => {
      if (props.quota > 0) {
        props.unlockItem(item_id);
      }
    }
  })
)(YourComponent)

One of the advantages of withHandlers is that it passes the same method to YourComponent every re-render, allowing it to optimize out of re-renders if it implements shouldComponentUpdate.

17reactions
markeriksoncommented, Nov 1, 2016

On the flip side, I personally tend to write a method on the component that explicitly takes a value from props and calls a bound action creator:

onDoSomeThingClicked() {
    const {someItemID} = this.props;
    this.props.doSomeThing(someItemID);
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

Access State inside of mapDispatchToProps method
I think the real use case for accessing the state in mapDispatchToProps is to know which actions are available at runtime. For example...
Read more >
How to access props in mapDispatchToProps - Emma Goto
Accessing props from state using mergeProps. While using Redux, you may come across a situation where you are passing in props from both ......
Read more >
Connect: Dispatching Actions with mapDispatchToProps
This is the only way to trigger a state change. With React Redux, your components never access the store directly - connect does...
Read more >
Redux: What is Connect()? - DEV Community ‍ ‍
function connect(mapStateToProps, mapDispatchToProps)(ComponentName) · (ComponentName) · const mapStateToProps = (state) => { return { users: ...
Read more >
Manage state with React Hooks - IBM
Use a React Class Component. · Implement mapDispatchToProps to have access to the dispatch object to call actions. · Implement mapStateToProps to have...
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