在子组件中,我将道具设置如下:
function mapStateToProps(state) {
return {
user : state.App.get('user'),
foo: 'ok'
}
}
但是,当我在父组件的this.props.children
方法中阅读render
时,foo
不在那里。谁知道为什么?
我意识到我无法在项目中看到来自父母的孩子状态。我错过了什么吗?
我也在这款应用中使用Redux。怎么了?在父组件中执行this.props.children.foo
的正确方法是什么?
答案 0 :(得分:1)
你不应该引用this.props.children.foo
来获取foo。将组件与mapStateToProps
相关联后,您应该可以从this.props.foo
this.props.children
将检索React组件的开始和结束标记中包含的所有元素。
React文档页面上有一个CodePen示例说明了这一点: http://codepen.io/gaearon/pen/ozqNOV?editors=0010
function WelcomeDialog() {
return (
<FancyBorder color="blue">
/*everything between these two blocks*/
<h1 className="Dialog-title">
Welcome
</h1>
<p className="Dialog-message">
Thank you for visiting our spacecraft!
</p>
/*is what is sent to FancyBorder as this.props.children*/
</FancyBorder>
);
}
来源:https://facebook.github.io/react/docs/composition-vs-inheritance.html