【问题标题】:pass {...this.state} with {this.props.children}通过 {...this.state} 和 {this.props.children}
【发布时间】:2016-11-19 02:09:05
【问题描述】:

大家好,感谢您的宝贵时间。

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

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

但我使用的是新版本的 React 15.2.1 和 React-router 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>
    )
}

谢谢大家

【问题讨论】:

标签: javascript reactjs react-router


【解决方案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>
    )
}

【讨论】:

    【解决方案2】:

    又是我,我必须添加一个发射 在教程中,使用旧版本的 React。

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

    现在,如何添加发射?

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

    谢谢,

    【讨论】:

    • 编辑您的原始问题,不要将编辑作为答案发布,您在此处提供了答案(而事实并非如此)
    猜你喜欢
    • 2018-10-04
    • 1970-01-01
    • 2020-02-22
    • 2016-06-20
    • 2017-04-02
    • 2019-11-12
    • 1970-01-01
    • 2020-08-18
    • 1970-01-01
    相关资源
    最近更新 更多