ReactJs。获取儿童组件

时间:2016-02-20 21:57:25

标签: javascript reactjs

我是Bootstrap Panel的组件包装器:

var Panel = React.createClass({
    render: function () {
        return (
            <div className="panel panel-default">
                <div className="panel-heading">
                    <div className="panel-title">
                        {this.props.title}
                    </div>
                </div>
                <div className="panel-body"></div>
            </div>
        );
    }
});

如何在示例中输出“panel-body”标签“h1”和组件“AvailableActions”,您可以在下面看到这些内容?

var PlayerActions = React.createClass({
    render: function () {
        return (
            <Panel title="Actions">
                <h1>Some description here...</h1>
                <AvailableActions></AvailableActions>
            </Panel>
        );
    }
});

2 个答案:

答案 0 :(得分:5)

好像你需要this.props.children

var Panel = React.createClass({
    render: function () {
        return (
            <div className="panel panel-default">
                <div className="panel-heading">
                    <div className="panel-title">
                        {this.props.title}
                    </div>
                </div>
                <div className="panel-body">{ this.props.children }</div>
            </div>
        );
    }
});

Example

答案 1 :(得分:2)

<div className="panel-body">
    {this.props.children}
</div>