Solid Props — SolidJS

Props are sent like attributes (<Greeting name="Andi" />) and received as one object, function Greeting(props), then read in JSX: <h2>Hello…

<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" />
}

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();
  }
})
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>
  )
}

⚠️ Props that freeze, go missing, or never get read

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).

What you will learn