Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

reactjs

React.createElement takes a spread "children" parameter

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

but I can't find any documentation on how to actually use it

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

I'm sure this has a really really simple answer.

like image 892
George Mauer Avatar asked May 12 '15 15:05

George Mauer


People also ask

Is it possible to use React without JSX?

JSX is not a requirement for using React. Using React without JSX is especially convenient when you don't want to set up compilation in your build environment. Each JSX element is just syntactic sugar for calling React.

How do you pass children to component in React?

Children are props If you want to pass things to this button, you would use a prop. If you want to make our button say more than just "I am a button," you can pass children to it. By passing children in this way, you are passing it to the component by position.

Does JSX allows us to put HTML elements in DOM without using appendChild () or createElement () method?

JSX allows us to write HTML elements in JavaScript and place them in the DOM without any createElement() and/or appendChild() methods. JSX converts HTML tags into react elements.


1 Answers

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

like image 69
Michelle Tilley Avatar answered Sep 22 '22 21:09

Michelle Tilley