Skip to content
Tutorials Center
My learning

Topics

Lesson 19 of 39 ยท Components and Props

Children and Composition in SolidJS

Compose SolidJS components with props.children, the children helper, named slots, render props and callback children, and avoid duplicate rendering.

Components become powerful when they can wrap other content: a card with any body, a layout with a sidebar and a main area, a list that lets you decide how each row looks. This lesson covers the three composition tools in Solid: children, slots (named props) and render props, plus the children helper that makes them safe.

props.children

Whatever you put between a component's tags arrives as props.children.

JSX
function Panel(props) {
  return (
    <section class="panel">
      <h3>{props.title}</h3>
      {props.children}
    </section>
  );
}

<Panel title="Profile">
  <p>Name: Ada</p>
</Panel>

Children can be text, elements, other components, arrays, or even functions. Solid defines children as a lazy getter: the nodes are created at the moment you read props.children, in the reactive context where you read it. That is useful (you can place it inside Show, so the children are only created when needed) and also a trap.

A Panel that wraps any content

Collapse and expand the panels: the children inside Show are created and removed with the condition.

The reading-twice trap

Because props.children builds nodes on every read, this component creates the content twice:

JSX
function Twice(props) {
  return (
    <>
      <header>{props.children}</header>
      <footer>{props.children}</footer>   // a second copy of the DOM
    </>
  );
}

A DOM node can live in only one place, and effects inside the children run twice. If you must read children more than once, for example to inspect or count them, resolve them first with the children helper.

The children helper

children(() => props.children) evaluates the children once, caches the real nodes in a memo, and gives you an accessor:

JSX
import { children } from "solid-js";

function Tabs(props) {
  const resolved = children(() => props.children);
  createEffect(() => {
    resolved.toArray().forEach((el) => el.classList?.add("tab"));
  });
  return <div class="tabs">{resolved()}</div>;
}
  • resolved() returns the resolved node (or an array).
  • resolved.toArray() always returns a flat array, handy for counting or iterating.
  • Because the nodes are resolved once, reading resolved() twice does not duplicate DOM.

Use the helper when you want to look at children, add attributes or classes to them, or place them in more than one reactive spot.

Counting and decorating children

Named slots as props

When a component has several areas, pass each as a prop that holds JSX. This is Solid's version of slots.

JSX
function Layout(props) {
  return (
    <div class="layout">
      <aside>{props.sidebar}</aside>
      <main>{props.children}</main>
      <footer>{props.footer}</footer>
    </div>
  );
}

<Layout sidebar={<Nav />} footer={<small>(c) 2026</small>}>
  <Article />
</Layout>

Each slot is read once in its own place, so there is no duplication. Props holding JSX behave like children: they are created when read.

Render props and callback children

A render prop is a function the child calls with data, letting the parent decide the markup. It is how For, Show and Match work.

JSX
function Hover(props) {
  const [over, setOver] = createSignal(false);
  return (
    <div onMouseEnter={() => setOver(true)} onMouseLeave={() => setOver(false)}>
      {props.children(over)}
    </div>
  );
}

<Hover>{(over) => <p>{over() ? "hovering" : "hover me"}</p>}</Hover>

Pass accessors (like over) to the function, not plain values, so what the parent builds stays reactive. This is the same reason For hands you index as a function.

Composition over configuration

Prefer many small components that nest to a long list of boolean props. For example, a Card with Card.Header, Card.Body is simpler to read and extend than one with showHeader, headerText, bodyAlign. In Solid, composition is also cheaper: every component is just a function that runs once.

Common mistakes

  • Reading props.children twice (duplicate DOM and effects). Use the children helper.
  • Calling children() at the top level of the body and expecting it to update. Wrap or memoize it.
  • Passing values instead of accessors to render props, which freezes them.
  • Treating children as an array. It may be a node, a string, an array or a function; use toArray() after resolving.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What is special about props.children in Solid?

    Children is a lazy getter, so reading it twice builds the nodes twice.
  2. 2. What does the children helper give you?

    It resolves and caches the nodes, so you can read or inspect them safely.
  3. 3. How do you give a component several named content areas?

    Props like sidebar and footer can hold JSX, giving slot-like behavior.
  4. 4. In a render prop, what should you pass to the callback to keep things reactive?

    Accessors keep the parent's markup subscribed to the child's state.

Summary

  • props.children holds nested content and is created when read.
  • Use the children helper to resolve children once and inspect or reuse them.
  • Named props (sidebar, footer) act as slots; function children are render props.
  • Pass accessors, not values, from render props.
  • Prefer composition of small components over many configuration flags.

Next: Forms and Controlled Inputs