Rendering children with CreateElement
children is a special property in React.
Let’s make sure we know how they work.
- Log the
elementto the console (so we can see what a React Element is made of)$$typeof: Symbol(react.element)type: "div"- A few special props like
keyandref - And our
propsobject withchildrenandclassName— as we wrote them
Now we can add children in many different ways.
- Remove the
childrenproperty,- And add the “Hello React” string asa third argument
- This is functionally identical
But we can also children with different types and get the same result!
- Overload the function by splitting up the string into two:
Hello, andReact- The output to the DOM remains the same but
childrenis now an array of two strings - And we can add as many items as we want to this array
- The output to the DOM remains the same but
Now this behavior isn’t limited to function overloading.
We can pass an array to the children property for the same result.
Finally, any of these elements can be another React element.
- Let’s empaphize “React” by wrapping it in a
React.elementwith:- The element type
em - and no additional props (
null)
- The element type
Looking at the logged React element, we see:
- Our
react.element, with- a two element
childrenarray- the first: a string “Hello”
- and the second:
- another
react.element, - with the type
em, - and children: the string
React
- another
- a two element
We learned that:
createElementrecieves children in multiple ways,- as additional function arguments
- or a single array,
- and that children can also accept arrays with additional React elements
<body> <div id="root"></div> <script type="module">
const rootElement = document.getElementById('root') const element = React.createElement( 'div', { // children: "Hello React", // children: ["Hello", " React"], children: [ 'Hello', React.createElement('em', null, ' React'), ], className: 'greeting', } // "Hello", // " React" // ["Hello", " React"] )
ReactDOM.createRoot(rootElement).render(element) </script></body>