<BookCard title="The Rainbow Troops" stock={stock()} /> looks like an HTML tag with two attributes. What's really happening is a parent component sending two pieces of data to its child, BookCard. Data sent this way is called props. In Solid, the child receives them all in one object called props, and reads them with a dot: props.title, props.stock.
How you read them matters, and this is where Solid differs from React. Think of a book card on the shelf with a small window that peeks at the stock ledger in the storeroom. Every time someone glances through the window, they see the latest number in the ledger. If the number were copied onto the card with a pen instead, that copy would never change even when the ledger does. Reading props.stock is glancing through the window. Destructuring, ({ stock }), is copying with a pen.
Sending and receiving props
function Greeting(props) {
return <h2>Hello, {props.name}!</h2>
}
function App() {
return <Greeting name="Andi" />
}
<Greeting name="Andi" />sends a prop callednamewith the value"Andi". The caller decides the value.function Greeting(props)receives one object. It holds{ name: "Andi" }.{props.name}is read inside the<h2>, so it showsHello, Andi!. This is also the shape of this lesson's exercise answer.- Text is sent in quotes. Other values go in curly braces:
price={18},stock={stock()}. Writtenprice="18", what arrives is a string.
props is an object that reads again
import { createSignal } from "solid-js"
function BookCard(props) {
console.log("BookCard runs")
return (
<div class="card">
<h2>{props.title}</h2>
<p>Stock: {props.stock}</p>
</div>
)
}
function App() {
const [stock, setStock] = createSignal(4)
return (
<div>
<button onClick={() => setStock(s => s - 1)}>Buy 1</button>
<BookCard title="The Rainbow Troops" stock={stock()} />
</div>
)
}
The Solid 1.9 compiler writes that use of BookCard like this:
_$createComponent(BookCard, {
title: "The Rainbow Troops",
get stock() {
return stock();
}
})
titleis a plain value, because"The Rainbow Troops"never changes.stockbecomes a getter: a function that runs every time the property is read. So every timeprops.stockis read, Solid callsApp'sstock()right then.{props.stock}inside JSX is read again whenever thestocksignal changes, exactly like{stock()}.
| Event | stock signal in App |
Card (props.stock) |
Console |
|---|---|---|---|
| Page opens | 4 | Stock: 4 |
BookCard runs |
Click Buy 1 |
3 | Stock: 3 |
|
| Click again | 2 | Stock: 2 |
The BookCard function still runs once. Only {props.stock} in its JSX reads again.
Don't destructure
function BookCard({ title, stock }) {
return (
<div class="card">
<h2>{title}</h2>
<p>Stock: {stock}</p>
</div>
)
}
Before reading on, take a guess: the parent sends stock={stock()}, which starts at 4, then a Buy 1 button lowers the signal to 3. Does this card show Stock: 4 or Stock: 3?
Destructuring in the parameter reads props.stock once, when the BookCard function runs, and keeps the number in a plain variable. That function never runs again, so the stock variable stays 4 forever. After Buy 1, App is at 3, but the card still says Stock: 4.
The same happens with const { title, stock } = props in the component body, and with const initialStock = props.stock outside the JSX. They all read once. The rule: read props.something right where the value is used.
Defaults with mergeProps
When a prop isn't sent, its value is undefined and nothing shows: <BookCard title="Beauty Is a Wound" /> shows just Stock: . For a default value, use mergeProps:
import { mergeProps } from "solid-js"
function BookCard(props) {
const p = mergeProps({ stock: 0 }, props)
return (
<div class="card">
<h2>{p.title}</h2>
<p>Stock: {p.stock}</p>
</div>
)
}
mergeProps({ stock: 0 }, props)merges a default object with props. Ifprops.stockexists, that's used. If not,0.- The result is still an object that reads again, so
{p.stock}still follows the parent's signal. Don't destructure the result either:const { stock } = mergeProps(...)reads once, same as before. - Its sibling,
splitProps(props, ["title"]), splits props into two groups (say, the ones used here and the rest to pass on) without losing the reading again.
⚠️ Props that freeze, go missing, or never get read
- Destructuring in the parameter.
function BookCard({ stock })shows the first value forever. Writefunction BookCard(props)and readprops.stock. - Keeping a prop in a variable first.
const s = props.stockin the component body also reads once. Read it directly in the JSX. - Prop names that don't match. The parent sends
name, the child readsprops.userName: the result isundefined, and the greeting becomesHello, !. - Typing the name straight into the heading.
<h2>Hello, Andi!</h2>looks right, but the exercise checks that the name is read fromprops.nameinside the<h2>.
TL;DR: Props are sent like attributes (<Greeting name="Andi" />) and received as one object, function Greeting(props), then read in JSX: <h2>Hello, {props.name}!</h2>. In Solid, a prop whose value comes from a signal is sent as a getter, so {props.stock} in JSX keeps changing even though the component function runs once. Destructuring (({ stock }) or const { stock } = props) reads once and cuts that reading again: the card gets stuck at the first value. For defaults, mergeProps({ stock: 0 }, props), and to split props, splitProps, both without losing reactivity, as long as their result isn't destructured.
⚡ Send through attributes, receive one object
Write:
function Greeting(props) {
return <h2>Hello, {props.name}!</h2>
}
function App() {
return <Greeting name="Andi" />
}
Meaning: <Greeting name="Andi" /> sends a prop called name. The child receives them all in one object, props, and reads them with a dot: {props.name}. Text is sent in quotes, other values in curly braces: stock={stock()}.
⚡ props reads again
Meaning: A prop whose value comes from a signal is sent as a getter. Every time props.stock is read, Solid calls the parent's stock() right then. So {props.stock} in JSX keeps changing even though the BookCard function runs once.
⚡ Don't destructure
Write:
function BookCard({ title, stock }) {
return <p>Stock: {stock}</p>
}
Guess first: the parent sends stock={stock()}, which starts at 4, then a Buy 1 button lowers it to 3. Does this card show Stock: 4 or Stock: 3?
Stock: 4, forever. Destructuring reads props.stock once, when the function runs, and that function never runs again.
Meaning: Destructuring cuts the reactivity of props. Read props.something right where the value is used. For a default value, use mergeProps({ stock: 0 }, props).