Redux 通过组合连接函数重用MapStateTrops
例如,我遇到这样的情况:三个组件使用来自存储的相同状态,以及它们唯一的状态 我想重构它,所以我不会重复工作,通过这样做:Redux 通过组合连接函数重用MapStateTrops,redux,react-redux,Redux,React Redux,例如,我遇到这样的情况:三个组件使用来自存储的相同状态,以及它们唯一的状态 我想重构它,所以我不会重复工作,通过这样做: const sharedMapStateToProps = state => ({ sharedValue1: state.sharedValue1, sharedValue2: state.sharedValue2, sharedValue3: state.sharedValue3, }); const uniqeMapStateToProps = st
const sharedMapStateToProps = state => ({
sharedValue1: state.sharedValue1,
sharedValue2: state.sharedValue2,
sharedValue3: state.sharedValue3,
});
const uniqeMapStateToProps = state => ({
uniqueValue1: state.uniqueValue1,
uniqueValue2: state.uniqueValue2,
});
compose(
connect(sharedMapStateToProps),
connect(uniqeMapStateToProps)
)(SomeComponent)
有没有办法重用和组合mapStateToProps
/mapDispatchToProps
/mergeProps
功能?您尝试过吗
compose(
connect(state => ({ ...sharedMapStateToProps(state), ...uniqeMapStateToProps(state) })),
)(SomeComponent)
我尝试了类似的方法:
const-mapStateToProps=state=>({
`…sharedMapStateToProps(state),`uniqueValue1:state.uniqueValue1,`uniqueValue2:state.uniqueValue2,`})代码>`