Compose content with children
Let the parent supply the content
The children prop lets a component accept nested JSX or text from its caller. Use it when the wrapper should own structure but the parent should decide the content. The wrapper must explicitly render children where that content belongs.
function Panel({ title, children }) { return ( <section> <h2>{title}</h2> {children} </section> );}
export default function App() { return ( <main> <h1>Supply desk</h1> <Panel title="Pickup"><p>Collect orders at the desk.</p></Panel> <Panel title="Hours"> <p>Monday to Friday</p> <p>9 am to 5 pm</p> </Panel> </main> );}Expected: Supply desk, then two sections with headings Pickup and Hours and their respective paragraphs. The first nested element becomes the children input. The second panel receives multiple elements. Do not assume children is always an array or a single object. Strings and numbers can also be rendered; empty children need not display content. Avoid adding a required wrapper just to inspect the shape of children.
Use named slots when positions matter
function ProductPanel({ name, children, footer }) { return ( <article> <h2>{name}</h2> {children} <footer>{footer}</footer> </article> );}
export default function App() { return ( <main> <h1>Featured items</h1> <ProductPanel name="Canvas" footer={<p>Pickup only</p>}> <p>Size: 20 by 30 cm</p> </ProductPanel> <ProductPanel name="Pencil" footer={<p>Delivery available</p>}> <p>Grade: HB</p> </ProductPanel> </main> );}Expected: Canvas with its size and Pickup only in its footer; Pencil with its grade and Delivery available in its footer. A JSX value is an element description that can be passed as a normal prop. Here footer is a named slot; children is the conventional main slot. The wrapper controls placement and semantics, while the parent supplies the content. Use explicit slots when the positions have different roles.
Recognize a missing children expression
function Panel({ title, children }) { return <section><h2>{title}</h2></section>;}
export default function App() { return <main><Panel title="Notice"><p>Desk closes at 5.</p></Panel></main>;}The code is valid JSX, but only Notice is expected to appear. The child paragraph is never included in the return value. The repair is to insert {children} after the heading, as in the first example. Avoid treating children as HTML strings or using raw HTML insertion; ordinary JSX composition preserves the intended React node representation.
Practice
Build an OrderPanel with a required title, a children slot for details, and a footer slot for a status message. Use it for two orders with different body structures. Explain how the parent can change the content without editing OrderPanel. Next: conditional rendering.