React Foundations · Composition

Composition vs props

Composition vs props

Props let you configure a component, but they are a blunt tool. Every new variation means another prop and another branch inside the component. A more flexible approach is composition: let the caller pass the pieces.

function App({ header, children }) {
  return (
    <div>
      <header>{header}</header>
      <main>{children}</main>
    </div>
  );
}

header and children are slots: the caller decides what goes in them. The component only says where each slot lands.

<App header={<h1>Title</h1>}>
  <p>Body</p>
</App>

Compare that with a title="Title" prop, which can only ever be text. A slot can hold any element — a heading, a button, another component — so the component stays small and the caller keeps control. A new piece of content is a new slot, not a new prop the component has to anticipate. Prefer composition when a prop would only be forwarding content.

Your job is to render a header slot and a body slot.

Challenge

A layout has slots for a header and a body, but it renders an empty box. Export an App component from App.jsx that takes a header prop and children, and renders the header inside a <header> element and the children inside a <main> element.

The Test passes an <h1> as the header and looks for it inside <header>. The starter renders an empty <div>, so the Test fails. Place each slot in its own element.

App.jsx
export function App() {
  return <div />;
}

No test run yet

The Test is hidden. Run it to check the Challenge — you only see the result, never the test source.