Home / Modern UI / Signals State Library

Signals State Library in JavaScript, Free with Live Demo

Free signals library in plain JavaScript. Build signal, computed and effect in about 40 lines, the idea behind Solid, Vue and Angular, with a live shopping cart demo.

Open live demoDownload HTML fileView code on GitHub
Signals State Library JavaScript project: a 40 line reactive state library running a shopping cart

Runs on: Signals, computed, effect. Every modern browser.

What is the Signals State Library?

Signals are how many modern frameworks know exactly what to update when data changes. Instead of redrawing the whole page, only the numbers that depend on a change are updated. Solid, Preact, Vue and Angular all use this idea.

This project writes a complete signals library in about 40 lines, then uses it to run a shopping cart. A counter next to each effect shows how many times it ran, so you can see that only the right parts update.

Good for

  • Small apps that need reactive state
  • Understanding how frameworks work inside
  • Job interviews about reactivity
  • Adding live updates to plain HTML

What this project does

How it works

  1. Track readsWhen an effect runs, every signal it reads adds that effect to its subscriber list.
  2. Notify on writeSetting a signal schedules each subscriber once, in a microtask, so many writes cause one update.
  3. Derive valuescomputed() is an effect that writes into its own signal, so it caches and updates only when its inputs change.

The key JavaScript

This is the heart of the project. The full file has the rest, including the screen layout and error handling.

let current = null;
export function signal(value) {
  const subs = new Set();
  const read = () => { if (current) subs.add(current); return value; };
  read.set = (v) => { if (v !== value) { value = v; subs.forEach(schedule); } };
  return read;
}
export function effect(fn) {
  const run = () => { const prev = current; current = run; try { fn(); } finally { current = prev; } };
  run();
}
export function computed(fn) {
  const s = signal(); effect(() => s.set(fn())); return s;
}

How to use it

  1. Click Download HTML file above.
  2. Open the file in a code editor, like VS Code.
  3. Run it from a local server with npx serve . so the camera, microphone and AI features are allowed.
  4. Change the text and colors, then upload it to GitHub Pages, Netlify or your own site. It is one file with no build step.

Questions people ask

What is a signal in JavaScript?

A signal is a value that remembers who reads it. When you change it, everything that read it runs again. computed() builds values from other signals, and effect() runs code when they change.

Are signals coming to JavaScript itself?

There is a TC39 proposal to add signals to the language. Until then, small libraries like this one, or Preact Signals, give you the same idea.

Is this fast enough for real apps?

For small and medium apps, yes. Updates are batched in a microtask, so ten writes in a row cause one update.

More Modern UI projects