Skip to content
Tutorials Center
My learning

Topics

Lesson 21 of 39 ยท Stores and Context

Stores with createStore

Learn how SolidJS createStore tracks nested objects and arrays property by property, how the path syntax of setStore works, and when to pick a store over signals.

Signals are perfect for a single value: a counter, a text field, a flag. But real applications hold structured data: a user with an address, a list of todos where each todo has a title and a done flag. You could wrap such data in one signal, but then every change replaces the whole object, and everything that read any part of it must re-run.

A store solves this. createStore gives you a nested, reactive object where each property is tracked on its own. Change user.address.city and only the code that read city runs again. In this lesson you will create stores, read them, update them with the setStore path syntax, and learn the rules that keep reactivity intact.

The live examples on this page load SolidJS from a CDN and use the html tagged-template helper (solid-js/html), which is the no-build-step cousin of JSX. The reactivity is identical. The read-only snippets use normal JSX, as you would write in a real project.

Creating and reading a store

createStore takes an initial object or array and returns a pair: the store (read it like a plain object) and a setter.

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

const [state, setState] = createStore({
  user: { name: "Ada", city: "London" },
  todos: [],
});

console.log(state.user.name); // "Ada"

Unlike a signal, you do not call the store as a function. You read properties directly. Behind the scenes the store is a Proxy: when code reads state.user.name inside an effect or a JSX expression, Solid records a subscription to that exact property. Nothing else is tracked.

flowchart LR
  S["state.user"] --> N["name"]
  S --> C["city"]
  N -. "read by effect A" .-> A["Effect A"]
  C -. "read by effect B" .-> B["Effect B"]

Each leaf property has its own subscribers. Writing city wakes only effect B.

Property-level tracking

Each effect runs once at start. Changing city runs only the city effect, and changing name runs only the name effect. With a single createSignal({ name, city }) both effects would run every time.

Updating with setState

The setter has a flexible path syntax: every argument except the last names a step into the object, and the last argument is the new value (or an updater function).

JSX
setState("user", "city", "Berlin");          // set a leaf value
setState("user", "city", (c) => c + "!");    // updater receives the previous value
setState("user", { city: "Rome", age: 36 }); // object: MERGED into user
setState("todos", (list) => [...list, t]);   // replace an array

Two rules are worth memorizing:

  1. Objects are merged, not replaced. setState("user", { age: 36 }) adds or changes age and leaves name and city alone. (Arrays are replaced; they are not merged.)
  2. You never mutate the store directly. state.user.name = "x" does nothing useful and logs a warning in development. All writes go through the setter.

You can also pass a top-level object to merge into the root: setState({ loading: true }).

Updating arrays

Arrays inside a store are reactive too. The path can contain an index, a filter function, or a range:

JSX
setState("todos", 1, "done", true);                    // by index
setState("todos", (t) => t.id === 3, "done", true);    // every item matching a predicate
setState("todos", { from: 0, to: 2 }, "done", false);  // a range of indexes
setState("todos", [0, 2], "done", true);               // a list of indexes
setState("todos", todos.length, newTodo);              // append at the end

The predicate form is the one you will use most: it finds items by id and updates one field, and only the UI bound to that field of that item changes.

A todo list updated by path

Tick the boxes: each click updates one done field, and Solid patches only that checkbox, that label style and the counter. Press Rename #2 and only that text node changes. The <li> elements are never recreated.

Why not just destructure?

Reactivity lives in the reading, so reading too early loses it:

JSX
const { name } = state.user; // reads once, name is now a plain string

name will never update. Keep reading through the store (state.user.name) inside JSX or an effect, or wrap it in a function: const name = () => state.user.name. This is the same "access late" rule you know from signals and props.

Store or signal?

Use a signal when... Use a store when...
The value is a primitive The data is a nested object or a list
The whole value is replaced at once Parts change independently
It is local and short-lived Many components share one data shape

Stores also give you stable identities: if you update one todo, the other todo objects are the same objects as before, so keyed lists do not recreate rows. The next lesson, Updating Stores: produce, reconcile and unwrap, shows tools for bigger updates.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does createStore return?

    createStore returns [state, setState]. You read the state like a normal object and write with setState.
  2. 2. After setState('user', { age: 40 }), what happens to user.name?

    Object values passed to setState are shallow-merged into the existing object.
  3. 3. Which line breaks reactivity?

    Destructuring reads the property once, outside a tracking scope, so later changes are not seen.
  4. 4. How do you update only the todo with id 3?

    A function in the path acts as a filter, so only matching items are updated.

Summary

  • createStore(initial) returns [state, setState]; read props directly, write with the setter.
  • Tracking is per property, so unrelated effects and DOM nodes stay untouched.
  • setState uses a path: keys, indexes, filter functions, ranges, then the value or an updater.
  • Objects merge; arrays are replaced; never mutate the store directly.
  • Do not destructure store values; read them where they are used.

Next: Updating Stores: produce, reconcile and unwrap