← Back to React, page 7

react-0138 · Hard senior

How does fine-grained reactivity (signals) differ from React's re-render model, and what are the trade-offs?

React re-runs component functions and diffs the output when state changes, so the unit of update is the component. Signal-based systems (Solid, Vue's refs, Angular signals, Preact signals) track which computations read each value and re-run only those when it changes, so the unit of update is the individual binding. Signals avoid re-render and memoization work, but React's model is simpler to reason about (UI is a function of state) and has a larger ecosystem. React Compiler narrows the gap with automatic memoization.

Full answer

Two ways of answering "what should update when this value changes?".

React: coarse updates by re-rendering. A state change schedules a render of that component and its children. React runs the functions and compares the new elements to the old ones (reconciliation), then updates the DOM where they differ. The programming model is simple: components are pure functions of props and state, and no dependency tracking is declared by you (except in hook dependency arrays). The cost is repeated work for unchanged parts, which memo, state colocation and the Compiler reduce.

Signals: fine-grained updates by tracking. A signal is a container for a value with a get and set. When a computation (a derived value or an effect that updates a piece of DOM) reads a signal, the system records that dependency. When the signal changes, it re-runs only the computations that depend on it, and updates just those DOM nodes. The component function in Solid runs once, and later updates are done by these tracked bindings. In Vue, ref and reactive do the same through proxies, and Angular signals provide it with change detection integration.

Trade-offs:

  • Performance: signals avoid diffing and unnecessary child execution, so they often scale better for frequently changing, fine-grained data. React's overhead is typically acceptable and is improving.
  • Mental model: React's re-render model means plain JavaScript semantics (values are snapshots). With signals you must remember that reading signal.value inside tracked scopes creates dependencies, and destructuring props or reading values outside tracking can lose reactivity.
  • Ecosystem and tooling: React has the largest library ecosystem, React Native and Server Components.
  • Control: React exposes render and commit semantics for concurrency, transitions and Suspense. Signal libraries are exploring similar features.

React Compiler memoizes components and values at build time, which brings some of the benefit of fine-grained updates without changing the model. Preact offers signals that can bind directly to the DOM within a React-like API.

The example is a short signal implementation that updates only the dependent effect.

jsx
let currentEffect = null;

function signal(initial) {
  let value = initial;
  const subscribers = new Set();
  return {
    get() {
      if (currentEffect) subscribers.add(currentEffect);
      return value;
    },
    set(next) {
      if (Object.is(next, value)) return;
      value = next;
      subscribers.forEach((run) => run());
    },
  };
}

function effect(fn) {
  const run = () => {
    currentEffect = run;
    fn();
    currentEffect = null;
  };
  run();
}

const firstName = signal('Asha');
const lastName = signal('Rao');
const counter = signal(0);

effect(() => console.log('name binding ran: ' + firstName.get() + ' ' + lastName.get()));
effect(() => console.log('counter binding ran: ' + counter.get()));

console.log('--- change counter');
counter.set(1);
console.log('--- change firstName');
firstName.set('Meera');
console.log('--- set firstName to the same value');
firstName.set('Meera');

This is plain JavaScript with a tiny signal model and no React. Each effect re-ran only when a signal it read changed. In React, the equivalent state change would run the component function again. Output of the run: name binding ran: Asha Rao counter binding ran: 0 --- change counter counter binding ran: 1 --- change firstName name binding ran: Meera Rao --- set firstName to the same value

Key points

  • React updates by re-rendering components and diffing. Signals track dependencies and update exact bindings.
  • Signals avoid repeated work and memoization, but need care to keep reads tracked.
  • React's model is simpler to reason about and has a larger ecosystem and concurrency features.
  • The React Compiler reduces unnecessary work without changing the model.
  • Choose by update patterns, team knowledge and ecosystem needs.

Common mistakes

  • Saying signals are always faster than React.
  • Destructuring reactive values and losing tracking in signal libraries.
  • Assuming React tracks dependencies automatically.
  • Ignoring the ecosystem and hiring side of the decision.
  • Comparing frameworks with micro-benchmarks only.

Follow-ups

  • How does the React Compiler narrow the gap with signals?
  • What happens to a signal-based component function after the first run?
  • How do Angular signals integrate with change detection?

Tips

  • Define the unit of update for each model.
  • Give one honest advantage for each side.
  • Mention the Compiler as React's answer.