简体   繁体   English

如何通过组件的Java评估传递道具?

[英]How to pass props through a Javascript eval of a component?

I've got the following code: 我有以下代码:

Layout = React.createClass({
  render() {
    return (
      <div>
        <header>
          <div className="container">
            <button className="btn btn-sm pull-right"
                    onClick={this.handleLogout}>LogOut
            </button>
          </div>
        </header>

        <div className="container">
          {this.props.content}
        </div>
      </div>
    );
  }
});

The Layout component serves as my shell. 布局组件用作我的外壳。 I pass it other "pages" which will get rendered with it (don't worry so much about this code, it's specific to Meteor): 我将其他的“页面”传递给它(不用担心此代码,它专用于Meteor):

ReactLayout.render(Layout, {content: <Home />});

My problem is, I want Layout to be able to pass props down to its children. 我的问题是,我希望Layout能够将props传递给它的子代。 In pseudo-code: 用伪代码:

<div className="container">
  {this.props.content someProp=this.props.someProp}
</div>

How do I go about doing this? 我该怎么做呢?

There a couple ways to go about passing the props to the children, but a simple way that we have a utility method for doing so is with React.cloneElement . 有几种方法可以将道具传递给孩子,但是我们有一个实用的方法可以通过React.cloneElement

renderChildren() {
  const someProps = { content : this.props.conent }// Filter props to pass
  return React.Children.map(this.props.children, function(child) {
    return React.cloneElement(child, someProps)
  });
}

render() {
//...
  <div className="container">
    {this.renderChildren()}
  </div>
//...
}

https://facebook.github.io/react/blog/2015/03/03/react-v0.13-rc2.html#react.cloneelement https://facebook.github.io/react/blog/2015/03/03/react-v0.13-rc2.html#react.cloneelement

As an example fiddle on how this looks and so you can test yourself: https://jsfiddle.net/jhuwhjp1/ 举个例子,弄弄它的外观,以便您可以测试自己: https : //jsfiddle.net/jhuwhjp1/

~~ Note: The previous way of doing this was React.addons.cloneWithProps which has been deprecated ~~注意:以前的实现方法是React.addons.cloneWithProps ,已弃用

Figured it out. 弄清楚了。 Points to cdbitesky for pointing me in the right direction. 指向cdbitesky以指向正确的方向。

This render function in Layout will take whatever content I pass it (a React component) and pass it any of Layout 's props. Layoutrender函数将接受我传递的任何内容(一个React组件),并将Layout的任何道具传递给它。

Layout = React.createClass({
  getDefaultProps() {
    /*
     * This will get passed down to whatever React component was passed to
     * Layout via the prop "content"
     */
    return {awesomeness: true}
  },

  render() {
    return (
      <div>
        <header>
          <div className="container">
            <button className="btn btn-sm pull-right"
                    onClick={this.handleLogout}>LogOut
            </button>
          </div>
        </header>

        <div className="container">
          {React.cloneElement(this.props.content, this.props)}
        </div>
      </div>
    );
  }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM