Skip to content
Tutorials Center
My learning

Topics

Lesson 8 of 39 ยท Signals and Reactivity

Batching, untrack and on

Control reactivity in SolidJS: group writes with batch, read without subscribing using untrack, and set explicit dependencies with on and defer.

Automatic tracking is Solid's best feature, but sometimes it tracks too much, or runs too often. Three small tools give you control: batch groups several writes into one update, untrack reads a signal without subscribing, and on lets an effect declare its dependencies explicitly.

batch: many writes, one update

Every call to a setter can trigger the effects that depend on it. If you change three signals in a row, an effect that reads all three could run three times, seeing half-updated state in between.

JavaScript
const [first, setFirst] = createSignal("Ada");
const [last, setLast] = createSignal("Lovelace");

createEffect(() => console.log(first(), last()));

setFirst("Grace");
setLast("Hopper");
// without batch: logs "Grace Lovelace" and then "Grace Hopper"

Wrap the writes in batch and effects wait until the callback finishes:

JavaScript
batch(() => {
  setFirst("Grace");
  setLast("Hopper");
});
// logs once: "Grace Hopper"
Batched and unbatched writes

The unbatched button logs two lines, the first with a mix of new and old names. The batched one logs one consistent line.

Good to know:

  • Solid batches writes made inside effects, but not in event handlers, timers or promise callbacks (the demo above proves it). Use batch there when several signals change together.
  • Inside a batch, memos are not recomputed until the batch ends, so a memo read there can still show its old value.
  • Stores (see Stores with createStore) batch their own multi-field updates automatically.

untrack: read without subscribing

Sometimes an effect needs to read a signal's value but should not re-run when it changes. Wrap the read in untrack:

JavaScript
createEffect(() => {
  const query = search();                      // tracked: re-run when query changes
  const mode = untrack(() => settings().mode); // read once, not tracked
  fetchResults(query, mode);
});

Think of it as "give me the current value, and do not remember that I looked".

Tracked and untracked reads

You can pass a getter directly: untrack(lang) is the same as untrack(() => lang()). A common use is reading props once for an initial value:

JavaScript
const [draft, setDraft] = createSignal(untrack(() => props.initial));

Without untrack, tools such as the Solid ESLint plugin warn that you are reading props outside a reactive scope. With it, you state clearly that a one-time read is intended.

on: explicit dependencies

on(deps, fn) creates a function that tracks only the signals you list, and runs fn untracked. It reads like React's dependency array, but it is opt-in.

JavaScript
createEffect(on(count, (value, prev) => {
  console.log("count went from", prev, "to", value);
}));

The callback receives the new value and the previous value. You can list several signals as an array: on([a, b], ([a, b], [prevA, prevB]) => ...).

By default the effect runs once immediately. Use defer: true to skip the first run and react only to changes:

JavaScript
createEffect(on(userId, (id) => loadProfile(id), { defer: true }));
on with defer and previous value

Notice that changing other logs nothing: on does not track what the callback reads. The "deferred" effect stays silent until the first click.

Choosing a tool

Situation Tool
Several writes should look like one change batch
Read a value once, inside a reactive scope untrack
Run only when one specific signal changes on
React to changes but skip the first run on(..., { defer: true })
Need the previous value of a signal on callback's second argument

Common mistakes

  • Overusing untrack to silence bugs. If an effect needs a value to stay fresh, tracking it is correct.
  • Calling batch around async code. The batch ends when the callback returns; writes after an await are not inside it.
  • Expecting on to track everything. It tracks only its first argument, which is its whole point.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does batch do?

    Writes inside batch are applied together; subscribers run after the callback ends.
  2. 2. Inside an effect you call untrack(() => other()). What happens when other changes?

    untrack reads the value without creating a subscription.
  3. 3. What does { defer: true } do in on()?

    With defer the callback first runs when the tracked signal changes, not at creation.
  4. 4. Which values does an on() callback receive?

    The callback is called with (input, prevInput, prevValue).

Summary

  • batch groups writes so effects see one consistent update.
  • untrack reads a value without subscribing.
  • on(deps, fn) declares exactly what to track; defer: true skips the first run.
  • Use these tools sparingly: automatic tracking is right most of the time.

Next: Ownership and Cleanup