How to use `React.createElement` children parameter (without jsx)

前端 未结 1 989
无人共我
无人共我 2021-01-31 02:43

React.createElement takes a spread \"children\" parameter

var d = React.DOM;

React.createElement(LabeledElement, {label: \"Foo\"}, 
     d.input({v         


        
相关标签:
1条回答
  • 2021-01-31 03:22

    The children passed to a component, either via JSX nesting or via the third+ argument to React.createElement, shows up in the component as 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"})
          )
        );
      }
    });
    

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

    0 讨论(0)
提交回复
热议问题