Component slots and render props

Props can hold more than strings and numbers. A slot receives finished markup, a render prop receives a function that turns the component's data into markup.

Named slots

Besides children, any prop can hold JSX, so a card can offer a slot for its header and one for its footer. A slot is an ordinary prop: give it a default value as a fallback.

function Card({ header, footer = <small>No footer</small>, children }) {
  return (
    <article>
      <header>{header}</header>
      {children}
      <footer>{footer}</footer>
    </article>
  );
}

<Card header={<strong>Jeasx 3.0</strong>} footer={<a href="#">Changelog</a>}>
  <p>Faster streaming and smaller bundles.</p>
</Card>

<Card header={<strong>No footer given</strong>}>
  <p>The default value renders instead.</p>
</Card>
Jeasx 3.0

Faster streaming and smaller bundles.

No footer given

The default value renders instead.

No footer

Render props

If the markup needs data from the component, pass a function instead. Here the async <Jokes> component loads the data, and the caller decides how it looks: the same loader renders a list and cards.

async function Jokes({ amount, children }) {
  const jokes = await loadJokes(amount);
  return children(jokes);
}

<Jokes amount={2}>
  {(jokes) => (
    <ul>
      {jokes.map((joke) => <li>{joke.setup} <em>{joke.punchline}</em></li>)}
    </ul>
  )}
</Jokes>

<Jokes amount={2}>
  {(jokes) => jokes.map((joke) => (
    <article>
      <p>{joke.setup}</p>
      <p><em>{joke.punchline}</em></p>
    </article>
  ))}
</Jokes>
  • When a dad drives past a graveyard: Did you know that's a popular cemetery? Yep, people are just dying to get in there
  • What's red and bad for your teeth? A Brick.

What do you call an elephant that doesn’t matter?

An irrelephant.

What do you call corn that joins the army?

Kernel.