Server components and islands → Signals and fine-grained reactivity
2020 – today · 2021 – today
What changed
- Where the state lives
Server components and islands
In the island's
useState, started from the props the server serialized next to it. The static parts have no state at all.Signals and fine-grained reactivity
In signals: the list and the filter are signals, and each todo's
doneis a signal of its own.leftand each row'sshownarecomputed.- How the page is rendered
Server components and islands
The server renders the whole page to HTML; only the island's element is hydrated with
hydrateRoot(). React never touches the static HTML.Signals and fine-grained reactivity
Plain DOM: a row is built once, when its todo appears. After that only effects touch it; there is no virtual DOM and no template re-render.
- What happens on a change
Server components and islands
Inside the island a setter re-renders
TodoAppand React updates the DOM that differs; the rest of the page never changes.Signals and fine-grained reactivity
Setting
.valuere-runs only the effects that read that signal, right away: ticking a todo updates its row's class and the counter text, nothing else.- How the code is organised
Server components and islands
Server-only components that never reach the browser, around client islands that each have their own root, script and props.
Signals and fine-grained reactivity
Signals and computed values at the top, then small effects, each bound to one piece of the DOM.
Why it changed
What hurt in each era along the way, and the era that came next.
Server components and islands→Signals and fine-grained reactivity
Inside an island, and inside every client component, nothing changed: each state change runs the component again and React compares the new output with the old. Ticking one checkbox here re-renders the whole
TodoAppand compares every item; keeping large islands fast takesmemo,useMemoanduseCallback. Signals and fine-grained reactivity track which piece of the DOM reads which value and update only that.
The same app, side by side
The main file of each version. Both apps pass the same test: same markup, same behaviour, different ideas.
The apps run in sandboxes in your browser.
Server components and islands
React 19.3.0, React DOM 19.3.0
/server.jsx · 48 lines
// Islands era: the server renders the whole page to HTML, and only the islands get JavaScript.
// An islands framework such as Astro does this for you: you only mark what is interactive.
import { renderToStaticMarkup, renderToString } from "react-dom/server";
import TodoApp from "./islands/TodoApp.jsx";
// The "database" lives on the server.
const todos = [
{ id: 1, title: "Read the docs", done: true },
{ id: 2, title: "Write the app", done: false },
];
// A server-only component: it runs here and leaves plain HTML behind. It ships NO JavaScript:
// this file is never sent to the browser, and nothing hydrates its HTML.
function Intro() {
return (
<>
<h1>Todos</h1>
<p>This heading and note are plain HTML with no JavaScript. Everything below them is an island.</p>
</>
);
}
// "<" is escaped so that a todo titled "</script>" cannot end the script element early.
function serialize(props) {
return JSON.stringify(props).replace(/</g, "\\u003c");
}
export async function handle({ method, path }) {
if (method !== "GET" || path !== "/") return { status: 404, body: "<h1>Not found</h1>" };
const props = { initialTodos: todos };
// Static part: HTML only, without the markers hydration needs. No JavaScript.
const intro = renderToStaticMarkup(<Intro />);
// The island: HTML that hydrateRoot can attach to, with its props as JSON right next to it.
const island = renderToString(<TodoApp {...props} />);
return {
status: 200,
body: `
<main class="todo-app">
${intro}
<div id="todo-island">${island}</div>
<script id="todo-island-props" type="application/json">${serialize(props)}</script>
</main>
<script src="/client.jsx"></script>`,
};
}Signals and fine-grained reactivity
Preact Signals 1.14.5
/src/main.js · 78 lines
// Signals era: state lives in signals, and each effect keeps one piece of the DOM in sync.
// An effect remembers which signals it read and re-runs only when one of them changes.
import { signal, computed, effect } from "@preact/signals-core";
let nextId = 1;
// Each todo's done is a signal of its own, so ticking one todo touches only its own row.
const createTodo = (title, done = false) => ({ id: nextId++, title, done: signal(done) });
const todos = signal([createTodo("Read the docs", true), createTodo("Write the app")]);
const filter = signal("all");
// A computed is cached: it is recomputed only after a signal it read has changed.
const left = computed(() => todos.value.filter((t) => !t.done.value).length);
const list = document.getElementById("todo-list");
const rowTemplate = document.getElementById("todo-row");
// Builds a row once. From then on only its own two effects touch it.
function renderRow(todo) {
const li = rowTemplate.content.firstElementChild.cloneNode(true);
const checkbox = li.querySelector("[data-toggle]");
li.querySelector(".title").textContent = todo.title;
checkbox.addEventListener("change", () => {
todo.done.value = checkbox.checked;
});
// Re-runs only when THIS todo's done changes.
effect(() => {
li.classList.toggle("done", todo.done.value);
checkbox.checked = todo.done.value;
});
// Recomputed when the filter or this todo's done changes. If the answer stays the same
// (ticking a todo under "All"), the effect that reads it does not run at all.
const shown = computed(() => filter.value === "all" || (filter.value === "done") === todo.done.value);
effect(() => {
li.style.display = shown.value ? "" : "none";
});
return li;
}
// Re-runs only when the list itself changes (a todo is added). It builds rows for new todos
// and leaves the existing ones alone. Nothing is ever removed here, so no effect is disposed.
const rendered = new Set();
effect(() => {
for (const todo of todos.value) {
if (rendered.has(todo)) continue;
rendered.add(todo);
list.append(renderRow(todo));
}
});
// Re-runs when left changes: after adding or ticking a todo, never after picking a filter.
const count = document.getElementById("todo-count");
effect(() => {
count.textContent = `${left.value} left`;
});
// Re-runs when the filter changes: moves the "selected" class.
const buttons = document.querySelectorAll("[data-filter]");
effect(() => {
for (const button of buttons) button.classList.toggle("selected", button.dataset.filter === filter.value);
});
for (const button of buttons) {
button.addEventListener("click", () => {
filter.value = button.dataset.filter;
});
}
document.getElementById("todo-form").addEventListener("submit", (event) => {
event.preventDefault();
const input = document.getElementById("new-todo");
const title = input.value.trim();
if (!title) return;
// A new array, so the signal gets a new value and the effects that read it re-run.
todos.value = [...todos.value, createTodo(title)];
input.value = "";
});