2015-05-12 10 views
17

React.createElement prende uno spread parametro "figli"Come usare `React.createElement` parametro bambini (senza JSX)

var d = React.DOM; 

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

ma non riesco a trovare alcuna documentazione su come utilizzare effettivamente è

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

Sono sicuro che questo ha una risposta davvero molto semplice.

risposta

31

I bambini passati ad un componente, tramite JSX nidificazione o tramite il terzo + argomento React.createElement, presenta nel componente come 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"}) 
    ) 
    ); 
    } 
}); 

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

+0

Ah grazie. Non ho mai pensato di guardare * dentro * di "oggetti di scena" –

Problemi correlati