-
-
Notifications
You must be signed in to change notification settings - Fork 3.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Access state props in mapDispatchToProps
#535
Comments
You can't access state in mDTP. But you can in mergeProps, so your 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 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 |
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);
} |
Also, to better answer your question: while I don't have a specific reference off the top of my head, I think the main reason |
Great answers folks! Lots of alternative ways to deal with it. And now I understand the reasons mentioned regarding performance. Thanks! |
Another solution: <div onClick={()=>this.props.onMyAction(this.state)}>MyAction</div> function mapDispatchToProps(dispatch, ownProps) {
return {
onMyAction: (state) => {
dispatch(createAction(state.actionType));
}
}
} |
@msangel If you must, I would suggest providing only the specific properties of the state-props that you need in the dispatch-prop function. Also, keep in mind we're referring to state-props from react-redux, and not component state, as you have written <div onClick={() => this.props.onMyAction(this.props.actionType)}>MyAction</div>
function mapDispatchToProps(dispatch, ownProps) {
return {
onMyAction: (actionType) => {
dispatch(createAction(actionType))
}
}
} or if you prefer named args <div onClick={() => this.props.onMyAction({ actionType: this.props.actionType })}>MyAction</div>
function mapDispatchToProps(dispatch, ownProps) {
return {
onMyAction: ({ actionType }) => {
dispatch(createAction(actionType))
}
}
} |
What I do is having my action accept a new value to change the state, but in case I pass a function I call that function passing the state as parameter (local state in this example because I use a pointer like path but is not relevant here)
hope this help |
@Raising Thanks for sharing! I would avoid passing a complex object such as a function, or anything that cannot be serialized to a JSON string. In my experience, there is rarely/never a need for this pattern, and you are only setting yourself up for incompatibilities in the future, not to mention debugging headaches from day 1. I would also claim, ideally, your reducers should be pure functions that do not rely on any external function calls such as you have. Out of curiosity though, have you encountered a situation where none of the other proposed solutions have worked for you, and you 100% had to pass a function in the action payload? Thanks again! |
@TSMMark THanks for answering! I think you are right and my aproach may be easy at first but will lead to problems in the future. I have used this only on two spots in my project so I'll find another solution to prevent being unable to serialice the actions. Thanks for your insight!! |
I was resolving this problem and end up adding a bit extra of functionality. the code looks like this. export default (state = defaultState, action) => {
thanks you, you inspirated me to improve |
I was wondering why it is not possible to access state-derived props in
mapDispatchToProps
. For instance, given the followingmapStateToProps
function:I would like to access
props.quota
in the correspondingmapDispatchToProps
:And if it is possible, how. Because I tried and could not. And I could not find anything about it in the documentation.
The text was updated successfully, but these errors were encountered: