在父组件中看不到子道具

时间:2016-11-01 13:00:00

标签: reactjs redux react-router react-redux

在子组件中,我将道具设置如下:

function mapStateToProps(state) {
  return {
    user : state.App.get('user'),
    foo: 'ok'
  }
}

但是,当我在父组件的this.props.children方法中阅读render时,foo不在那里。谁知道为什么?

我意识到我无法在项目中看到来自父母的孩子状态。我错过了什么吗?

我也在这款应用中使用Redux。怎么了?在父组件中执行this.props.children.foo的正确方法是什么?

1 个答案:

答案 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

相关问题