Skip to content
Tutorials Center
My learning

Topics

Lesson 3 of 39 ยท Getting Started

How SolidJS Works: Compiler and Reactive Graph

Understand what the Solid compiler produces, how signals track dependencies automatically, and why the same DOM nodes survive every update.

Solid feels simple to write, but it rests on two ideas that explain almost every rule you will meet later: a compiler that turns JSX into plain DOM operations, and a runtime that tracks which pieces of code read which signals. Once you see both, "why can't I destructure props?" and "why does this effect run twice?" stop being mysteries.

Step 1: the compiler builds real DOM

When Vite compiles this component:

JSX
function Hello(props) {
  return <p class="greeting">Hello, {props.name}</p>;
}

the JSX is not turned into calls that describe an element (as a virtual-DOM library does). Solid generates something conceptually like this. It is simplified, but shows the shape:

JavaScript
const template = document.createElement("template");
template.innerHTML = '<p class="greeting">Hello, </p>';

function Hello(props) {
  const el = template.content.firstChild.cloneNode(true); // runs once
  const text = document.createTextNode("");
  el.appendChild(text);
  createRenderEffect(() => { text.data = props.name; });   // re-runs on change
  return el;
}

Two things matter here:

  1. The static HTML is parsed once into a template and then cloned, which is one of the fastest ways to create DOM.
  2. Every in JSX becomes a small effect that updates just that text node or attribute.

So a component with ten dynamic spots owns ten tiny effects, each wired to a single DOM node.

The code above is an illustration, not literal compiler output. Real output uses helpers like template(), insert() and effect() from solid-js/web, but the idea is the same: build once, then patch individual nodes.

Step 2: signals track who reads them

When code runs inside an effect, Solid keeps a "current listener". A signal getter checks for that listener when it is called and remembers it. Changing the signal later notifies every remembered listener.

Automatic dependency tracking

Try this order and read the console:

  1. Press change name and birthday while details are hidden. The effect does not run, because it only read showDetails() last time, so it only depends on that.
  2. Press toggle details. Now the effect reads name() and age() as well, so it depends on all three.
  3. Press change name again. The effect re-runs.

Dependencies are discovered dynamically each time the effect runs. You never write a dependency array.

Step 3: the reactive graph

flowchart LR
  S1["signal: count"] --> M["memo: double"]
  S1 --> E1["effect: log"]
  M --> R1["render effect: text node A"]
  S2["signal: name"] --> R2["render effect: text node B"]

Signals sit at the top, derived values and effects are subscribers below them. Changing count updates only what hangs beneath it.

Changing name runs one render effect that touches one text node. Nothing else in the component is visited. This is why Solid scales well: the work per update depends on how many things depend on the changed value, not on how big the page is.

Step 4: the same nodes live on

Because the component function ran once, the DOM it created is never thrown away just to apply a change. Prove it with an element identity check:

The DOM node survives updates

After any number of clicks the answer stays true. Solid changed the text inside the button but never replaced the button. That means focus, scroll position, text selection and CSS transitions on that element are all preserved, for free.

The two phases of a component

Every Solid component has two different kinds of code:

Phase Runs Examples
Setup Once createSignal, createMemo, onMount, local variables, the return of JSX
Reactive Many times the expressions inside {} in JSX, createEffect bodies, createMemo bodies

A frequent beginner mistake is to put logic in the setup phase and expect it to update:

JSX
function Bad(props) {
  const label = "Hello " + props.name;   // computed once, never updates
  return <p>{label}</p>;
}

function Good(props) {
  const label = () => "Hello " + props.name; // function: re-evaluated when read
  return <p>{label()}</p>;
}

The fix is to wrap the calculation in a function so it is evaluated inside a reactive spot. This is the single most important habit in Solid, and the lesson on derived values goes deeper.

What the runtime does not do

  • It does not re-run your component. If you add console.log at the top of a component, you will see it once.
  • It does not compare trees. There is no tree to compare.
  • It does not batch by guessing. Updates are synchronous unless you group them yourself with batch, covered in Batching and untrack.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does the Solid compiler do with the static parts of your JSX?

    Static markup becomes an HTML template that is cloned, which is fast and happens once per component instance.
  2. 2. An effect read signal A, but not signal B. What happens when B changes?

    Dependencies are the signals actually read during the last run. B was not read, so B is not a dependency.
  3. 3. Why does const label = 'Hi ' + props.name in the component body not update?

    The component body is setup code. Wrap the expression in a function, or use a memo, so it is evaluated reactively.
  4. 4. After a signal changes, what happens to the DOM node that displays it?

    Render effects write straight into the existing node, so focus, selection and transitions survive.

Summary

  • The compiler turns JSX into cloned templates plus small effects bound to individual nodes.
  • Reading a signal inside an effect subscribes that effect; dependencies are found automatically on each run.
  • Updates touch only the nodes whose signals changed, and DOM nodes are never recreated for a simple change.
  • Components have a setup phase (once) and reactive spots (many times); wrap derived calculations in functions.

Next: SolidJS vs React and Other Libraries