Access state props in `mapDispatchToProps`
See original GitHub issueI 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:
- Created 7 years ago
- Reactions:16
- Comments:10 (3 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
You can’t access state in mDTP. But you can in mergeProps, so your
connect
would look something like:I myself typically use
recompose
library to do is a slightly different way. I’d do:One of the advantages of
withHandlers
is that it passes the same method toYourComponent
every re-render, allowing it to optimize out of re-renders if it implements shouldComponentUpdate.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: