Composition and children

What this is. Whatever you put between a component's opening and closing tags arrives as a prop called children.

This is how you write a wrapper without deciding in advance what goes inside it.

Try it

Put a button inside the Panel. It does not need to know what a button is; it just renders whatever it is given.

loading editor…

loading React…

Why this matters more than it looks. The alternative to composition is a component with twelve props deciding what to show. That component becomes harder to change every time it is used. A wrapper that takes children stays the same size no matter how many different things go inside it.

When you find yourself adding a boolean prop to control whether some markup appears, ask whether the caller should just pass that markup in.

Passing functions down. A child cannot change its parent's data, but it can be handed a function to call. That is the standard way for a child to report that something happened, and you will use it constantly once state arrives in the next module.

◈ Graded Component

Build a Result component that takes a title and a year and renders them inside an article: the title in an h3 and the year in a paragraph reading Published YEAR. Then render exactly three Results from App, using the titles Kindred, Beloved and Wild Seed with the years 1979, 1987 and 1980.

Checked against what your component renders and how it responds, not against how you wrote it. 3 visible and 2 hidden checks.

loading editor…

Mark this lesson complete

Signed in, your progress follows you to every device.

Next: Practice Test →
← Back