Skip to content
Tutorials Center
My learning

Topics

Lesson 22 of 39 ยท Stores and Context

Updating Stores: produce, reconcile and unwrap

Use produce for mutable-style updates, reconcile to diff fresh server data into a store, and unwrap to get plain objects. Includes createMutable and its trade-offs.

The path syntax from the previous lesson is precise and efficient, but it can feel verbose when an update touches many fields. And when fresh data arrives from a server, you rarely know which fields changed. Solid ships three helpers in solid-js/store for these situations: produce, reconcile and unwrap. This lesson also covers createMutable, the "just mutate it" store, and explains why most teams avoid it.

produce: write updates like plain mutations

produce takes a function that receives a draft of the store and lets you use normal JavaScript mutation. Solid records the changes and applies them through the setter, so reactivity stays fine-grained.

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

const [state, setState] = createStore({
  cart: { items: [], coupon: null },
});

setState(
  produce((draft) => {
    draft.cart.items.push({ sku: "A1", qty: 1 });
    draft.cart.coupon = "WELCOME";
  })
);

You can also scope it to one branch of the path: setState("cart", produce((cart) => { cart.items.pop(); })).

produce shines for multi-step edits, array methods (push, splice, sort) and logic with branches. It does not copy the whole object like Immer does; it wraps the store proxy, so it is cheap.

produce for multi-step edits

Each produce call runs in one batch, so each effect fires once per call, not once per statement.

reconcile: diff new data into the store

Imagine you poll an API every few seconds and get a fresh array of users. If you do setState("users", newUsers), you replace every object, so every row in a <For> is recreated and every input loses focus. reconcile instead diffs the new data against the old and only writes what actually changed. Objects that survive keep their identity.

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

setState("users", reconcile(fetchedUsers));          // match by `id` (default key)
setState("users", reconcile(fetched, { key: "uuid" })); // another key name
setState("users", reconcile(fetched, { merge: true })); // diff deeper, keep more identity

By default reconcile uses the id property to match array items, so reordered items are recognized as the same items. Items that disappeared are removed, new ones are added, and unchanged fields are not touched.

reconcile keeps object identity

Only the score effect re-runs, because name did not change, and the original Ana object is still the one in the store. If you had used setState("users", payload), both effects would run, and Ana would be a brand new object.

Use reconcile whenever you copy server data into a store: polling, websocket updates, refetching after a mutation. It keeps inputs focused, animations running and <For> rows alive.

unwrap: get the plain object back

A store value is a proxy. Most code does not care, but some APIs do: structuredClone, postMessage, IndexedDB and some libraries cannot handle proxies. unwrap returns the underlying plain object.

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

const plain = unwrap(state);
localDb.put(plain);

unwrap does not copy. The result is the same object Solid uses internally, so treat it as read-only or you will bypass reactivity. For a copy, clone the unwrapped value.

structuredClone and unwrap

createMutable: the mutation-style store

createMutable returns one object you can mutate freely, like in MobX or Vue:

JSX
import { createMutable, modifyMutable, reconcile } from "solid-js/store";

const state = createMutable({ count: 0, user: { name: "Ada" } });
state.count++;               // reactive
state.user.name = "Grace";   // reactive
modifyMutable(state.user, reconcile({ name: "Linus" }));

It is convenient, but the Solid team recommends createStore for application state:

  • Writes can happen anywhere, so it is hard to see who changed what.
  • Passing the object to a child component hands over write access too.
  • It does not fit one-way data flow, which makes code harder to reason about.

createMutable is useful when migrating code written around mutation or when integrating with libraries that mutate objects. For new code, default to createStore and expose small setter functions instead.

Choosing the right update tool

Situation Tool
Change one or two known fields Path syntax
Many steps, array methods, branching logic produce
Replace data from a server reconcile
Hand data to a non-Solid API unwrap

The next lesson, Context API, shows how to share a store with a whole subtree without passing it through every component.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What problem does reconcile solve?

    reconcile compares the new data with the old and only writes differences, preserving object identity and avoiding needless DOM work.
  2. 2. Which key does reconcile use to match array items by default?

    The default is id. Pass { key: 'other' } to change it.
  3. 3. Why would you call unwrap?

    Stores are proxies. unwrap returns the raw object, useful for structuredClone, postMessage or storage APIs.
  4. 4. Why do many teams prefer createStore over createMutable?

    Mutable stores allow writes from any code that holds the object, which weakens one-way data flow.

Summary

  • produce lets you write updates as plain mutations, applied in one batch.
  • reconcile diffs incoming data (matching on id by default) so unchanged objects and DOM stay put.
  • unwrap returns the raw object; treat it as read-only.
  • createMutable allows direct mutation but is discouraged for app state.

Next: Context API