Command Palette

Search for a command to run...

Hectal
Back to the lesson: Topic 2.5 — The children Prop and Composition
React · Example 3 of 3

Several slots: a page layout

Layout owns the grid; the parent decides what goes in each area. Leave out sidebar and the aside disappears; leave out footer and a default text appears. Clicking a menu link is handled inside the preview.

Whatever you write between a component's opening and closing tags arrives as the children prop, so a component can wrap content it knows nothing about; extra props that hold JSX give it more named slots, and building screens by nesting such pieces (composition) beats piling up configuration props.

Change the code and press Run (Ctrl+Enter). Predict what will happen first, then break it on purpose and read what changes. Your edits are saved and match the lesson page.

Ready for more? This phase has 5 hands-on exercises with automatic checks.

Practice questions

Write the code in the editor, run it, then open the model answer to compare.

01

Write a Box wrapper that renders its children inside a div with a coloured border whose colour comes from a color prop (default "teal").

02

Write a Split layout with left and right slot props that shows them side by side, and use it with a list on the left and a paragraph on the right.

Explain it without notes

01

What is the children prop and how is it filled?

02

How do you build a component with more than one slot?

03

What does "composition over configuration" mean? Give an example.

04

How can composition help you avoid prop drilling?

Several slots: a page layout

Preview

Press Run to see the page.

Console

Nothing logged yet.