Skip to content
Tutorials Center
My learning

Topics

Lesson 5 of 39 ยท Signals and Reactivity

Creating Signals with createSignal

Master createSignal in SolidJS: read and write values, use the updater form, update arrays and objects safely, and control equality with the equals option.

A signal is the smallest unit of state in Solid. It holds one value and remembers everyone who has read it, so that when the value changes those readers are told. Everything else in the library, from effects to stores to resources, is built on top of this idea. In this lesson you will use createSignal in all its forms.

Getter and setter

createSignal takes an initial value and returns a two-element array: an accessor (the getter) and a setter.

JavaScript
import { createSignal } from "solid-js";

const [count, setCount] = createSignal(0);

count();          // 0
setCount(5);      // writes 5
count();          // 5

Call the getter to read; call the setter to write. Reading the getter inside a reactive scope, such as a JSX expression, an effect or a memo, creates a subscription. Reading it anywhere else, such as in a click handler or at module level, just returns the current value without subscribing.

Read and write a signal

The updater form

When the new value depends on the old one, pass a function to the setter. It receives the previous value and returns the next:

JavaScript
setCount(prev => prev + 1);

This is the safest style when several updates may happen in a row, because each function sees the result of the one before it.

A signal cannot hold a function directly: setFn(() => 1) would be treated as an updater and call it. To store a function as the value, wrap it: setFn(() => myFunction).

Signals hold any type

Strings, numbers, booleans, null, objects, arrays, DOM nodes, class instances: anything works. A typed example in TypeScript:

TypeScript
const [user, setUser] = createSignal<{ name: string } | null>(null);
const [ids, setIds] = createSignal<number[]>([]);

Objects and arrays: replace, do not mutate

By default a signal notifies its subscribers only when the new value is different from the old one, compared with ===. If you mutate an object and then write the same object back, nothing changes as far as the signal can tell:

JavaScript
const [todos, setTodos] = createSignal([]);

todos().push("buy milk");   // mutates in place, nobody is notified!
setTodos(todos());          // same reference: ignored

The correct way is to create a new array or object:

JavaScript
setTodos(prev => [...prev, "buy milk"]);                 // add
setTodos(prev => prev.filter(t => t !== "buy milk"));    // remove
setTodos(prev => prev.map(t => t.id === 3 ? { ...t, done: true } : t)); // change one
A list signal updated immutably

For deeply nested data, replacing whole objects gets tedious. That is what stores are for; signals shine for single values and for data you replace wholesale.

Controlling equality

The second argument to createSignal is an options object. The most useful option is equals.

  • equals: false makes the signal notify on every write, even with the same value. This is handy when you mutate an object and want to announce it.
  • equals: (a, b) => ... supplies your own comparison, for example to ignore case or to compare objects by id.
The equals option

Each effect logs once at startup. Setting "hello" on the plain signal logs nothing. The equals: false signal logs every time. The case-insensitive signal treats "HELLO" as equal to "hello", so it also stays quiet.

Signals and reads in different places

Where you call count() Tracked?
JSX expression {count()} Yes, updates that DOM spot
Inside createEffect or createMemo Yes
In an event handler No, just returns the value
In the component body (setup) No, runs once
Inside untrack(...) No, on purpose

Practical tips

  • Name getters and setters as a pair: [isOpen, setIsOpen], [query, setQuery].
  • Use several small signals rather than one large object signal when parts change independently.
  • Give a signal a name option ({ name: "cart" }) to see it in Solid's devtools.
  • Write through the setter only; never try to assign count = 5.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does createSignal return?

    The first element reads the value (call it), the second writes it.
  2. 2. Which line adds an item to an array signal so subscribers are notified?

    A new array is a different value, so the signal notifies. Mutating in place leaves the reference unchanged.
  3. 3. What does { equals: false } do?

    With equals false the signal never considers a write a no-op.
  4. 4. How do you store a function as a signal's value?

    A function passed to the setter is treated as an updater, so wrap the function you want to store in another function.

Summary

  • createSignal(initial) returns [getter, setter]; call the getter to read.
  • Use setX(prev => next) when the new value depends on the old one.
  • Replace arrays and objects instead of mutating them; default equality is ===.
  • The equals option changes when subscribers are notified.
  • Reads are tracked only inside reactive scopes.

Next: Side Effects with createEffect