Signals — SolidJS

const [count, setCount] = createSignal(0) creates a signal: count() reads, setCount(...) writes. A setter takes a value (setCount(count() + 1)) or an updater…

The number in the cart has to go up every time a buyer presses +1 Book. A plain variable isn't enough: count++ does change the number, but nothing tells the page. In Solid, a value that changes lives in a signal, the smallest container for a reactive value. If you come from React, it sits where useState would, only you read it differently.

createSignal(0) gives you two things: a getter to read the value and a setter to replace it. Every place that reads the getter in JSX is noted by Solid, and when the setter writes a new value, only those places are updated. It's like the cart number board at the till of Nadia's Bookshop, with a list of names beside it. Anyone who glances at the number is automatically put on the list. When the number changes, Nadia tells only the people on that list, not everyone in the shop. Glancing at the number is calling the getter, count(). Changing the number is the setter, setCount(...). Solid keeps the list itself.

createSignal: getter and setter

import { createSignal } from "solid-js"

function App() {
  const [count, setCount] = createSignal(0)

  return (
    <div>
      <button onClick={() => setCount(c => c + 1)}>+1</button>
      <p>Count: {count()}</p>
    </div>
  )
}

That's exactly this lesson's first exercise. You may write the import line, and the exercise editor already provides createSignal too.

Setter: a value or an updater function

A setter takes a new value, or a function that computes the new value from the old one (called an updater):

setCount(count() + 1)   // value: read it now, add 1
setCount(c => c + 1)     // updater: Solid passes the old value as c
setCount(1)              // fixed value: always 1

Only the text that reads it changes

This is Nadia's Bookshop's cart with a total price:

function App() {
  const [count, setCount] = createSignal(0)
  console.log("App runs")

  return (
    <div>
      <h2>The Rainbow Troops · $18</h2>
      <button onClick={() => setCount(c => c + 1)}>+1 Book</button>
      <p>Cart: {count()} books</p>
      <p>Total: ${count() * 18}</p>
    </div>
  )
}
Event count() Text nodes replaced Console
Page opens 0 App runs
Click +1 Book 1 0 to 1, 0 to 18
Click again 2 1 to 2, 18 to 36

Two places read count(), so each click replaces exactly two text nodes. The book title and the button aren't touched, and App doesn't run again.

A getter is called with parentheses

Take a guess first: the cart above gets the paragraph <p>Total: ${count * 18}</p>, with no parentheses after count. What shows: Total: $0, Total: $NaN, or an error?

count (no parentheses) is the function, not the number. A function times a number is NaN, so Total: ${count * 18} shows Total: $NaN, with no error. The number also never changes, because there's no count() read to follow.

Other results of forgetting the parentheses, tested in Solid 1.9:

Written Shows
{count()} 0, then follows changes
{count} 0, then follows changes (Solid accepts a function in this spot, but that's luck)
{count + 1} function () { [native code] }1
{`${count} books`} function () { [native code] } books
console.log(count) the function, not the number

So always write count(). A {count} that happens to show isn't the right way, and the moment the value is used in a calculation, the result breaks.

An input that writes to a signal

function App() {
  const [name, setName] = createSignal("World")

  return (
    <div>
      <input value={name()} onInput={e => setName(e.target.value)} />
      <h1>Hello, {name()}!</h1>
    </div>
  )
}

Use onInput, not onChange. In Solid, onChange uses the browser's own change event, which only fires when the box is left (blur), not on every keystroke as in React.

⚠️ Numbers that turn NaN, stick at 1, or never move

TL;DR: const [count, setCount] = createSignal(0) creates a signal: count() reads, setCount(...) writes. A setter takes a value (setCount(count() + 1)) or an updater (setCount(c => c + 1)); a fixed value like setCount(1) stops at 1. A signal changes right then, and only the text that reads its getter is replaced, while the component function doesn't run again. A getter is always called with parentheses: without them, {count * 18} shows NaN. For an input, onInput={e => setName(e.target.value)} then show {name()}; onChange in Solid only fires when the box is left.

⚡ createSignal: getter and setter

Write:

const [count, setCount] = createSignal(0)

<button onClick={() => setCount(c => c + 1)}>+1</button>
<p>Count: {count()}</p>

Meaning: createSignal(0) gives two functions: count() reads, setCount(...) writes. The text that reads count() changes right away every time the setter writes a new value, and the component function doesn't run again.

⚡ Setter: a value or an updater

Write:

setCount(count() + 1)   // value: read it now, add 1
setCount(c => c + 1)     // updater: Solid passes the old value as c
setCount(1)              // fixed value: always 1

Meaning: The first two lines both add 1 per click. setCount(1) gets stuck at 1, so an exercise waiting for Count: 2 fails.

⚡ A getter is called with parentheses

Guess first: the cart gets <p>Total: ${count * 18}</p>, with no parentheses after count. What shows?

Total: $NaN, with no error. count without parentheses is the function, not the number, and a function times a number is NaN.

Meaning: Always write count(). A {count} that happens to show isn't the right way, because the moment the value is used in a calculation, the result breaks.

⚡ An input that writes to a signal

Write:

const [name, setName] = createSignal("World")

<input value={name()} onInput={e => setName(e.target.value)} />
<h1>Hello, {name()}!</h1>

Meaning: onInput runs on every keystroke and stores the box's content in the signal, so the <h1> follows. Use onInput, not onChange: in Solid, onChange only fires when the box is left.

What you will learn