Signals

A signal holds a value that changes over time. createSignal returns a pair: a getter that reads the value and a setter that writes it.

const [count, setCount] = createSignal(0);
count(); // read: 0
setCount((value) => value + 1); // write, from the previous value

When JSX calls count(), that spot in the DOM subscribes to the signal. A write updates exactly those spots. The component function does not run again: open the preview console and notice that "Counter set up" is logged only once, however many times you click.

Try it: create a count signal, show count() in the button, and increment it on click with onClick.

Loading editor…