-
-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Props lost if mapStateToProps & mapDispatchToProps reference same high level prop #324
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
Comments
React Redux can’t guess how to merge two completely different objects. It isn’t obvious: for example, if both were Immutable maps, “merging” them naïvely would break them completely. We let you specify a third argument called const mapStateToProps = (state,ownProps) => {
return {
login: loginStateSelector(state)
};
};
const mapDispatchToProps = (dispatch, ownProps) => {
return {
loginUser: (creds) => {
dispatch(UserActions.loginUser(creds))
},
showModal: (modalName) => {
dispatch(UserActions.showModal(modalName))
}
};
};
const mergeProps = (stateProps, dispatchProps, ownProps) => {
return {
...ownProps,
redux: {
state: stateProps,
actions: dispatchProps
}
};
};
export default connect(
mapStateToProps,
mapDispatchToProps,
mergeProps
); I am using object spread operator in this example but you can write it with const mergeProps = (stateProps, dispatchProps, ownProps) => {
return Object.assign({}, ownProps, {
redux: {
state: stateProps,
actions: dispatchProps
}
});
}; I hope this helps! Note that grouping objects like this will cause unnecessary allocations and will also make performance optimizations harder because we can no longer rely on shallow equality of result props as a way to tell whether they changed. So you will see more renders than with a simple approach without namespacing which we recommend in the docs. |
Fix `bindActionCreators` example
I was hoping to group my redux state and dispatch props under a single property in my component, but my state props are lost when redux completes its merge.
I expected to see this
But, after redux combines the props, i see this
Is this a bug or working as designed? it seems that redux should be able to combine the two without loss of data.
The text was updated successfully, but these errors were encountered: