A Shelf component wants to show the book cards its caller sends, and also say how many there are. Reading props.children twice looks harmless, and if you come from React, there it is. In Solid, props.children isn't finished content. It's a getter: every time it's read, Solid makes the children from scratch, runs the components inside again, and makes new DOM elements.
The children(() => props.children) helper reads it once, then keeps the result so you can read it as often as you like. It's like Nadia ordering a shelf's contents from a publisher. If every time she asks "what's in it?" the publisher prints a brand-new set of books, the storeroom fills up with books that are never shown. children() takes the delivery once, then counts and displays from the same box.
props.children is a getter
function BookCard(props) {
console.log("BookCard runs:", props.title)
return <div class="card">{props.title}</div>
}
function Shelf(props) {
return (
<section class="shelf">
<p>On the shelf: {props.children.length} books</p>
<div class="contents">{props.children}</div>
</section>
)
}
function App() {
return (
<Shelf>
<BookCard title="The Rainbow Troops" />
<BookCard title="This Earth of Mankind" />
</Shelf>
)
}
Tested in Solid 1.9.17, the console:
BookCard runs: The Rainbow Troops
BookCard runs: This Earth of Mankind
BookCard runs: The Rainbow Troops
BookCard runs: This Earth of Mankind
Four lines, for two cards. The page is right, On the shelf: 2 books and two cards, but one set of cards was made only to be counted and then thrown away. Solid's compiler turns the content of <Shelf>...</Shelf> into a children getter, so props.children.length makes one set, and {props.children} makes a second.
children(): resolve once, read many times
import { children } from "solid-js"
function Shelf(props) {
const cards = children(() => props.children)
return (
<section class="shelf">
<p>On the shelf: {cards.toArray().length} books</p>
<div class="contents">{cards()}</div>
</section>
)
}
Now the console has only two lines, one per card.
children(() => props.children)takes a function that readsprops.children. Call it once, in the component's body.cards()returns the finished result: the element itself when there's one child, an array when there are more. However often it's read, it holds the same elements.cards.toArray()always returns an array, handy for.lengthor a loop.
The shape of this lesson's exercise:
import { children } from "solid-js"
function Wrapper(props) {
const resolved = children(() => props.children)
return <div class="wrapper">{resolved()}</div>
}
function App() {
return (
<Wrapper>
<p>Child content</p>
</Wrapper>
)
}
Wrapperdoesn't know its content. It provides a<div class="wrapper">and puts its caller's content there, so right afterclass="wrapper">comes{.Appuses<Wrapper>and wraps<p>Child content</p>in it. The result is<div class="wrapper"><p>Child content</p></div>.
Working with children: why resolve them first
The shelf wants to mark every card with an on-shelf class:
function Shelf(props) {
const cards = children(() => props.children)
createEffect(() => {
cards.toArray().forEach((el) => el.classList.add("on-shelf"))
})
return <section class="shelf">{cards()}</section>
}
Tested in Solid 1.9.17: both cards on the page become class="card on-shelf". If the effect wrote props.children.forEach(...) instead, that read would make a new set of cards, and the class would land on cards that never show. The cards on the page would stay class="card". That's what "resolving the lazy evaluation" means: props.children is only made when it's read, and children() turns it into real DOM elements you can count, loop over and work with.
If your component only puts {props.children} once in its JSX, you don't need this helper. Use it when you read the children more than once, or want to inspect and work with them.
⚠️ Cards made twice, and children() called the wrong way
- Reading
props.childrentwice. The components inside run twice, and the elements you work with aren't the ones that show. children(props.children)with no function. The helper needs a function, so this stops with a TypeError. Writechildren(() => props.children).- Calling
children()inside JSX or an effect. Each call reads the getter again. Call it once in the component's body, keep it in a variable, then read that variable. - The shape the exercise checks. Right after
class="wrapper">comes{...}, not typed text. And the caller writes<Wrapper>with no attributes, with content before</Wrapper>.
TL;DR: props.children in Solid is a getter: every read makes the children from scratch, so reading it twice runs the components inside twice (four BookCard runs lines for two cards). const resolved = children(() => props.children), called once in the component's body, resolves them into real elements: resolved() to show them, resolved.toArray() to count or work with them, and all of them are the same elements that show. The exercise: <div class="wrapper">{resolved()}</div> in Wrapper, used by App as <Wrapper><p>Child content</p></Wrapper>.
⚡ props.children is a getter
Guess first: a Shelf component reads props.children twice, once to count and once to show. Its content is two <BookCard />. How many times does BookCard runs print?
4 times. Every time props.children is read, Solid makes the children from scratch, so one set of cards is made only to be counted and then thrown away.
Meaning: In Solid, props.children isn't finished content. It's only made when it's read.
⚡ children(): resolve once, read many times
Write:
function Wrapper(props) {
const resolved = children(() => props.children)
return <div class="wrapper">{resolved()}</div>
}
Meaning: children takes a function that reads props.children, called once in the component's body. resolved() always holds the same elements, and resolved.toArray() gives an array to count or work with.
⚡ When you need this helper
Meaning: If your component only puts {props.children} once in its JSX, you don't need this helper. Use children() when the children are read more than once, or when you want to inspect and work with them, say to give them a class.