Stores in Solid — SolidJS

const [state, setState] = createStore({ name: "Andi", age: 25 }) from "solid-js/store". Read with a dot and no parentheses (state.age), tracked per property…

In the List Rendering lesson, the Discount The Rainbow Troops button replaced the book's object with a new one, and <For> took it for a new item. The card was rebuilt, and the gift note typed into it was gone. Yet only one number had changed, the price.

A store is made for data like this. It's a reactive container for objects and arrays: you read it like a plain object (shop.books[1].stock), but each of its properties is tracked on its own. createStore from "solid-js/store" makes one, and changes go through a setter that names the path to the part that changes. Picture Nadia's Bookshop's stock book, one line per book, with each card on the shelf peeking at its own line only. This Earth of Mankind sells, Nadia changes one number on This Earth of Mankind's line, and only the This Earth of Mankind card changes with it. If all the data sat in one signal, every change would mean rewriting the whole stock book on a new page.

createStore: a reactive object

Andi, a regular at Nadia's Bookshop, and his age:

import { createStore } from "solid-js/store"

function App() {
  const [state, setState] = createStore({ name: "Andi", age: 25 })

  return (
    <div>
      <p>{state.name} ({state.age})</p>
      <button onClick={() => setState("age", a => a + 1)}>+1 Year</button>
    </div>
  )
}
Event state.age Paragraph
Page opens 25 Andi (25)
Click +1 Year 26 Andi (26)

Path: pointing at the part that changes

Nadia's Bookshop's data in one store:

const [shop, setShop] = createStore({
  books: [
    { title: "The Rainbow Troops", price: 18, stock: 4 },
    { title: "This Earth of Mankind", price: 24, stock: 2 },
    { title: "Beauty Is a Wound", price: 19, stock: 0 },
  ],
  cart: [],
})

The setter's arguments read left to right like an address: the books property, element 1, the stock property, then the value. Tested in Solid 1.9:

Call Result
setShop("books", 1, "stock", s => s - 1) This Earth of Mankind's stock 2 becomes 1
setShop("books", 0, "price", 16) the price of The Rainbow Troops becomes 16
setShop("books", b => b.stock === 0, "stock", 5) a function picks elements: every sold-out book gets stock 5
setShop("books", 0, { stock: 9 }) the object is merged: stock becomes 9, title and price stay
setShop("cart", c => [...c, "The Rainbow Troops"]) the cart grows by one

Read per property

The Nadia's Bookshop shelf with a store, a note box on every card:

import { For } from "solid-js"
import { createStore } from "solid-js/store"

function App() {
  const [shop, setShop] = createStore({
    books: [
      { title: "The Rainbow Troops", price: 18, stock: 4 },
      { title: "This Earth of Mankind", price: 24, stock: 2 },
      { title: "Beauty Is a Wound", price: 19, stock: 0 },
    ],
  })
  const buy = (i) => setShop("books", i, "stock", s => s - 1)
  const discount = () => setShop("books", 0, "price", 16)

  return (
    <div>
      <button onClick={discount}>Discount The Rainbow Troops</button>
      <For each={shop.books}>
        {(b, i) => {
          console.log("row made:", b.title)
          return (
            <div class="card">
              <h3>{b.title}</h3>
              <p>${b.price}</p>
              <p>Stock: {b.stock}</p>
              <input placeholder="notes" />
              <button onClick={() => buy(i())} disabled={b.stock === 0}>Buy 1</button>
            </div>
          )
        }}
      </For>
    </div>
  )
}

The signal version keeps the same array in createSignal and changes it with a new object: setBooks(list => list.map((b, j) => (j === i ? { ...b, stock: b.stock - 1 } : b))). Type gift in the note of The Rainbow Troops, then click. Tested in Solid 1.9:

Click createSignal, new object createStore, path
Buy 1 on This Earth of Mankind row made: This Earth of Mankind, its whole card is made again 1 text replaced: Stock: 2 becomes Stock: 1
Discount The Rainbow Troops the card for The Rainbow Troops is made again, the gift note is gone 1 text replaced: $18 becomes $16, the gift note stays

With a store, the gift note stays. A store setter doesn't make a new object. It changes one property on the same object, so <For> sees the same item and leaves its row alone. The only thing that runs is the b.price read, the one reader of that property. Every property has its own readers: changing stock doesn't run the price read, and the other way round.

A list inside a store

An array inside a store is tracked too, its length included:

const [state, setState] = createStore({
  todos: ["Learn Solid", "Build a project"],
  input: "",
})

<p>Total: {state.todos.length} todos</p>
<ul>
  <For each={state.todos}>{(todo) => <li>{todo}</li>}</For>
</ul>

The paragraph reads Total: 2 todos, and the <ul> holds two <li>. The count is read from state.todos.length, not typed as the number 2, so it follows when a todo is added. This is the second exercise. state.todos.map(todo => <li>{todo}</li>) passes too, but <For> only makes rows for new items.

Store or signal?

A store's advantage: each property is tracked on its own, so a change to one property only runs that property's reads.

⚠️ A store that won't change, or can't be read

TL;DR: const [state, setState] = createStore({ name: "Andi", age: 25 }) from "solid-js/store". Read with a dot and no parentheses (state.age), tracked per property. Changed through the setter with a path: setState("age", a => a + 1), setShop("books", 1, "stock", s => s - 1); a function in the path picks elements, an object at the end is merged. Because the object stays the same, <For> doesn't remake the row, and only the reads of the changed property run: the note in the input box stays. Use a signal for one value, a store for nested objects and lists. A store can't be changed directly: shop.books[0].stock = 3 has no effect.

⚡ createStore: a reactive object

Write:

import { createStore } from "solid-js/store"

const [state, setState] = createStore({ name: "Andi", age: 25 })

<p>{state.name} ({state.age})</p>
<button onClick={() => setState("age", a => a + 1)}>+1 Year</button>

Meaning: createStore is imported from "solid-js/store". Its contents are read with a dot, without parentheses: state.age. Changes go through a setter that names the property, then the new value or a function that receives the old one.

⚡ Path: the address of the part that changes

Write:

setShop("books", 1, "stock", s => s - 1)

Meaning: The arguments read left to right like an address: the books property, element 1, the stock property, then the value. This Earth of Mankind's stock 2 becomes 1.

⚡ Tracked per property

Guess first: you type gift in the note on the card for The Rainbow Troops, then click Discount The Rainbow Troops. With a store, is the note still there?

Yes. A store setter doesn't make a new object, so <For> sees the same item and leaves its row alone. The only thing replaced is the text $18, which becomes $16.

Meaning: Each property of a store has its own readers, so changing price doesn't run the stock reads. A store can't be changed directly: shop.books[0].stock = 3 has no effect.

What you will learn