[英]React - override redux prop by parent
Saying i have a Container
connected to redux via: 说我有一个
Container
连接到redux通过:
const mapStateToProps = ({ MyReducer }) => ({
myProp: MyReducer.myProp
});
Is it possible to force the value of myProp by the parent (override redux)? 是否可以通过父级强制myProp的值(覆盖redux)?
I've tried: 我试过了:
<Container myProp={"notReduxValue"}/>
But mapStateToProps
overrides the provided value. 但
mapStateToProps
会覆盖提供的值。
NOTE: I can't change the container, my question is if it can be done via the parent only. 注意:我无法更改容器,我的问题是它是否只能通过父级完成。
(Of course this implies a bad state design but unfortunately it has come to that) (当然这意味着糟糕的状态设计,但不幸的是它已经到了)
Thanks 谢谢
mapStateToProps
accepts two arguments . mapStateToProps
接受两个参数 。 So I guess you could override it by: 所以我想你可以通过以下方式覆盖它:
const mapStateToProps = ({ MyReducer }, { myProp }) => ({
myProp: myProp || MyReducer.myProp,
})
In case you want to override myProp
in an upper level, you can use connect
's mergeProps
to do so (as @OrB also described). 如果你想覆盖
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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.