Javascript 带有动态子组件的ReactJS布局组件

Javascript 带有动态子组件的ReactJS布局组件,javascript,layout,dynamic,components,reactjs,Javascript,Layout,Dynamic,Components,Reactjs,使用React 0.12.2并给定布局组件,例如托盘: <div className="tray"> <div className="tray__item tray__item--left" data-width="260px"> Load a component in the left tray </div> <div className="tray__item tray__item--center">

使用React 0.12.2并给定布局组件,例如托盘:

<div className="tray">
    <div className="tray__item tray__item--left" data-width="260px">
        Load a component in the left tray
    </div>
    <div className="tray__item tray__item--center">
        Load a component in the center tray
    </div>
    <div className="tray__item tray__item--right" data-width="100%">
        Load a component in the right tray
    </div>
</div>

将零部件装入左侧托盘中
在中心托盘中加载零部件
将零部件装入右侧托盘中
我希望能够将任意组件插入到每个内容中,并将它们作为参数传递给该组件

也许是这样的:

<Tray left={Component1} center={Component2} right={Component3}/>

我还想知道如何传递未知数量的组件,例如:

<Carousel items={Component1,Component2,Component3,Component4}/>

需要明确的是——这些容器组件是“哑的”——它们只关心滑动内容——您应该能够将任何内容(组件)传递给它们


我如何才能做到这一点,然后渲染它们?谢谢。

您只需创建一个容器组件,该组件的渲染函数中有多个子组件。您永远不希望将组件作为道具传递进来,您应该创建一个容器组件,该容器组件的渲染函数中有多个子组件。您永远不希望将组件作为道具传递到Tray的渲染方法中

render: function() {
    return (
      <div className="tray">
        {this.props.children}
      </div>
    );
  }
render:function(){
返回(
{this.props.children}
);
}
然后在托盘所在的组件中,您可以执行以下操作

<Tray>
  <TrayItem position="left"/>
  <TrayItem position="center"/>
  <TrayItem position="right"/>
</Tray>

您应该能够继续嵌套此模式,即

<Tray>
  <TrayItem position="left">
     <SomeComponent/>
  </TrayItem>

  <TrayItem position="center">
     <div>
       <AnotherComponent/>
     </div>
  </TrayItem>

  <TrayItem position="right"/>
</Tray>

在这种情况下,TrayItem的渲染还应该包括
{this.props.children}


一般原则是,只要容器组件的渲染包含
{this.props.children}
,就可以将任意组件放入其他组件中

在托盘的渲染方法中,您可以执行以下操作

render: function() {
    return (
      <div className="tray">
        {this.props.children}
      </div>
    );
  }
render:function(){
返回(
{this.props.children}
);
}
然后在托盘所在的组件中,您可以执行以下操作

<Tray>
  <TrayItem position="left"/>
  <TrayItem position="center"/>
  <TrayItem position="right"/>
</Tray>

您应该能够继续嵌套此模式,即

<Tray>
  <TrayItem position="left">
     <SomeComponent/>
  </TrayItem>

  <TrayItem position="center">
     <div>
       <AnotherComponent/>
     </div>
  </TrayItem>

  <TrayItem position="right"/>
</Tray>

在这种情况下,TrayItem的渲染还应该包括
{this.props.children}


一般原则是,只要容器组件的渲染包含
{this.props.children}
,就可以将任意组件放入其他组件中

谢谢你的回答亚当·斯通+SimpleJ

var Tray = React.createClass({
    render: function () {
        return (
            <div className="tray">
                {this.props.children}
            </div>
        );
    }
});

var TrayItem = React.createClass({
    render: function () {
        return (
            <div className="tray__item">
                {this.props.children}
            </div>
        );
    }
});

<Tray>
    <TrayItem>
        <ComponentA/>
        <ComponentAB/>
    </TrayItem>
    <TrayItem>
        <ComponentB/>
    </TrayItem>
    <TrayItem>
        <ComponentC/>
    </TrayItem>
</Tray>
var-Tray=React.createClass({
渲染:函数(){
返回(
{this.props.children}
);
}
});
var TrayItem=React.createClass({
渲染:函数(){
返回(
{this.props.children}
);
}
});

谢谢你的回答亚当·斯通+SimpleJ

var Tray = React.createClass({
    render: function () {
        return (
            <div className="tray">
                {this.props.children}
            </div>
        );
    }
});

var TrayItem = React.createClass({
    render: function () {
        return (
            <div className="tray__item">
                {this.props.children}
            </div>
        );
    }
});

<Tray>
    <TrayItem>
        <ComponentA/>
        <ComponentAB/>
    </TrayItem>
    <TrayItem>
        <ComponentB/>
    </TrayItem>
    <TrayItem>
        <ComponentC/>
    </TrayItem>
</Tray>
var-Tray=React.createClass({
渲染:函数(){
返回(
{this.props.children}
);
}
});
var TrayItem=React.createClass({
渲染:函数(){
返回(
{this.props.children}
);
}
});

我不知道这些子组件是什么-组件是“哑”的,你怎么不知道它们是什么?你能举一个关于JSFIDLE的例子吗?如果我正在制作一个可重复使用的托盘组件,它唯一关心的是它可以滑动内容,它不关心内容是什么-我希望能够在多个位置使用该组件,并告诉它哪些组件将进入其中使用的位置。您应该有一个数据数组传递到其中,而不是完整的组件。我知道你要去哪里,但还没有完全明白。你到底在做什么?我不知道这些子组件是什么-组件是“哑的”,你怎么不知道它们是什么?你能举一个关于JSFIDLE的例子吗?如果我正在制作一个可重复使用的托盘组件,它唯一关心的是它可以滑动内容,它不关心内容是什么-我希望能够在多个位置使用该组件,并告诉它哪些组件将进入其中使用的位置。您应该有一个数据数组传递到其中,而不是完整的组件。我知道你要去哪里,但还没有完全明白。你到底在做什么?你可以像这样把数组作为属性传递:
items={[Component1,Component2,Component3,Component4]}
谢谢@SimpleJ-很明显!您可以将数组作为如下属性传递:
items={[Component1,Component2,Component3,Component4]}
谢谢@SimpleJ-很明显!