说我有一个Container
通过以下方式连接到redux:
const mapStateToProps = ({ MyReducer }) => ({
myProp: MyReducer.myProp
});
是否可以通过父级强制myProp的值(覆盖redux)?
我试过了:
<Container myProp={"notReduxValue"}/>
但是
mapStateToProps
会覆盖提供的值。注意:我无法更改容器,我的问题是它是否只能通过父级完成。
(当然,这意味着状态设计很差,但是不幸的是,这种情况已经到来了)
谢谢
最佳答案
mapStateToProps
接受two arguments。因此,我想您可以通过以下方法覆盖它:
const mapStateToProps = ({ MyReducer }, { myProp }) => ({
myProp: myProp || MyReducer.myProp,
})
如果您想在更高级别覆盖
myProp
,可以使用connect
的mergeProps
这样做(如@OrB所述)。const mapStateToProps = ({ MyReducer }, { myProp }) => ({
myProp: MyReducer.myProp,
})
const mapDispatchToProps = () => ({ ... })
const mergeProps = (stateProps, dispatchProps, ownProps) => ({
... // make your changes here
})
const ConnectedContainer = connect(
mapStateToProps,
mapDispatchToProps,
mergeProps
)(Container)
关于javascript - React-由父级覆盖Redux Pro,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/46523750/