如何使用`React.createElement` children参数(不带jsx)

时间:2015-05-12 15:46:50

标签: reactjs

React.createElement采用点差“子”参数

var d = React.DOM;

React.createElement(LabeledElement, {label: "Foo"}, 
     d.input({value: "foo"})
)

但我找不到任何关于如何使用它的文档

var LabeledElement = React.createClass({
    render: function() {
        return d.label({}
            ,d.span({classNames: 'label'}, this.props.label)
            , //How to place children here? 
    }
})

我确信这有一个非常简单的答案。

1 个答案:

答案 0 :(得分:48)

通过JSX嵌套或通过React.createElement的第三个+参数传递给组件的子组件在组件中显示为this.props.children

var MyLabel = React.createClass({
  render: function() {
    return React.createElement("label", {className: "label"},
      React.createElement("span", {className: "label"}, this.props.label),
      this.props.children
    );
  }
});

var App = React.createClass({
  render: function() {
    return React.createElement(MyLabel, {label: "Here is the label prop"},
      React.createElement("div", {},
        React.createElement("input", {type: "text", value: "And here is a child"})
      )
    );
  }
});

示例:http://jsfiddle.net/BinaryMuse/typ1f2mf/; docs:http://facebook.github.io/react/docs/multiple-components.html#children