可能的方法也是使用mergeProps
该合并mapState
并mapDispatch
允许同时使用两者。
// Define mapState
const mapState = (state) => ({
needeedValue: state.neededValue
})
// Define mapDispatch
const mapDispatch = (dispatch, ownProps) => {
return {
onChange: (newValue, neededValue) => {
dispatch(updateAttributeSelection('genre', newValue));
dispatch(getTableData(newValue, ownProps.currentYear, neededValue));
}
}
}
// Merge it all (create final props to be passed)
const mergeProps = (stateProps, dispatchProps, ownProps) => {
return {
...stateProps, // optional
...dispatchProps, // optional
onChangeWithNeededValue: (newValue) => (
dispatchProps.onChange(
newValue,
stateProps.needeedValue // <<< here the magic happens
)
)
}
}
// Pass mergePros to connect
const MyContainer = connect(mapState, mapDispatch, mergeProps)(MyComponent);
官方文档:react-redux#connect
大型应用程序可能存在的性能缺陷:Stack Overflow - Redux 中的 Performances and mergeProps