2016-07-15 57 views
1

大家好,感谢您的时间。通过{this.state} {this.props.children}

我看到一个教程与lynda.com React.js。 但是,它使用了旧版本的React。 他的代码:

render: function(){ 
    return (
    <div> 
     <Header title={this.state.title} status={this.state.status}/> 
     <RouteHandler {...this.state} /> 
    </div> 
    ) 
} 

但我使用的新版本15.2.1做出反应和应对路由器2.5.2。 而且我不知道如何通过{...} this.state用{} this.props.children

render: function(){ 
    return (
    <div> 
     <Header title={this.state.title} status={this.state.status}/> 
     {this.props.children} 
    </div> 
    ) 
} 

谢谢大家

+0

的可能的复制[阵营路由器和this.props.children - 如何通过状态this.props.children(http://stackoverflow.com/questions/35835670/react -router-and-this-props-children-how-to-pass-state-to-this-props-children) – azium

+0

你可以在JSFddle或codepen中发布一个包含你的代码的链接。从上面的例子很难理解。 –

回答

1

以一些道具传递给{this.props.children},你可以使用React.cloneElement。由此产生的元素将具有原始元素的道具与浅层合并的新道具。

render: function(){ 
    return (
    <div> 
     <Header title={this.state.title} status={this.state.status}/> 
     {React.cloneElement(this.props.children,this.state)} 
    </div> 
    ) 
} 
+0

完美,谢谢。 – Rifton007

0

这又是我,我要补充的EMIT 在教程中,与旧版本作出反应的。

<RouteHandler **emit={this.emit}** {...this.state}/> 

现在,如何添加发出?

{React.cloneElement(this.props.children, this.state)} //where add emit ? 

感谢,