React Reference ยท JSX Syntax and Rules
JSX children
Reference for JSX children: what you can nest between tags, how props.children works, which values render nothing, and how functions as children enable render props.
Whatever you write between an opening and a closing tag becomes the children prop of that element. On a DOM tag, children are the content of the element. On your own component, children is an ordinary prop that the component decides what to do with: wrap it in markup, place it in a slot, count it, or ignore it.
Many kinds of values are valid children. Strings and numbers become text. Elements are rendered. Arrays are rendered item by item. null, undefined, true and false are valid but render nothing, which is what makes {condition && <X />} work. A function is not a valid child for React itself, but a component can receive one and call it, the basis of the render props pattern.
function Card({ title, children }) { return ( <section> <h3>{title}</h3> {children} </section> ); }
Try it
Syntax
<Parent>text</Parent> // children === 'text' <Parent><A /></Parent> // children === element <Parent><A /><B /></Parent> // children === [elementA, elementB] <Parent>{(arg) => <A arg={arg} />}</Parent> // children === function <Parent children={x} /> // same as nesting, but not recommended
| Content | Result |
|---|---|
| One child | props.children is that value itself, not an array. |
| Several children | props.children is an array. |
| String or number | Rendered as text. |
| Element or array of elements | Rendered; array items need keys when the list is dynamic. |
null, undefined, true, false |
Valid, render nothing, but keep their position. |
Plain object, Date, Symbol |
Error or warning. |
| Function | Valid only if the receiving component calls it. |
Errors and edge cases: if both a children attribute and nested content are given, the nested content wins.
Usage notes
- One versus many. Because a single child is not wrapped in an array,
children.map(...)throws for the one-child case. Use the helpers inChildren(count,map,toArray) or avoid inspecting children at all. Children.countcounts empty slots too. It countsnullandfalsechildren;Children.toArray()drops them and also assigns keys.- Whitespace is trimmed. Lines of text and the blank space around tags on separate lines do not become text nodes. See JSX whitespace.
- Prefer composition over inspection. Instead of searching
childrenfor certain types, accept named props (header,footer) or use context. Code that clones or inspects children breaks when someone wraps a child in another component. - Function children. The calling component must call them with arguments and return the result. If nobody calls the function, React warns "Functions are not valid as a React child".
- TypeScript. Type
childrenasReactNodefor anything renderable, and as a function type for render props. - Children are not copied. The same element objects you pass in are the ones rendered, so you cannot change their props; use
cloneElement()for a modified copy. - Dynamic lists as children need keys, see JSX lists.