Hooks → Server components and islands
2018 – today · 2020 – today
What changed
- Where the state lives
Hooks
In
useState: the list lives in theuseTodos()hook, the filter and the text field in the component.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.- How the page is rendered
Hooks
React renders the whole
Appfrom state on every change and updates only the DOM that differs.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.- What happens on a change
Hooks
A handler calls a setter with a new array; React re-runs the component and recomputes
shownandleft.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.- How the code is organised
Hooks
A function component plus a custom hook: logic is grouped by feature and can be reused.
Server components and islands
Server-only components that never reach the browser, around client islands that each have their own root, script and props.
Why it changed
What hurt in each era along the way, and the era that came next.
Hooks→Server components and islands
Everything still ran in the browser: the page arrived empty, every component's JavaScript had to download, run and render before anything useful appeared, and server rendering hydrated the whole page even where nothing was interactive. Effects with dependency arrays and stale closures became a common source of bugs. Islands and React Server Components moved the non-interactive parts back to the server.
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.
Hooks
React 19.3.0, React DOM 19.3.0
/src/App.jsx · 73 lines
// Hooks era: function components hold state with useState; the UI is computed from it.
import { useState } from "react";
import { useTodos } from "./useTodos.js";
const FILTERS = { all: "All", active: "Active", done: "Done" };
export default function App() {
const { todos, add, toggle } = useTodos();
const [filter, setFilter] = useState("all");
const [title, setTitle] = useState("");
// Derived values are computed on every render, not stored as state.
const shown = todos.filter((t) => filter === "all" || (filter === "done") === t.done);
const left = todos.filter((t) => !t.done).length;
function handleSubmit(event) {
event.preventDefault();
const text = title.trim();
if (!text) return;
add(text);
setTitle("");
}
return (
<main className="todo-app">
<h1>Todos</h1>
<form id="todo-form" onSubmit={handleSubmit}>
<input
id="new-todo"
value={title}
onChange={(event) => setTitle(event.target.value)}
placeholder="What needs doing?"
autoComplete="off"
aria-label="New todo"
/>
<button id="add" type="submit">
Add
</button>
</form>
<ul id="todo-list">
{shown.map((todo) => (
<li key={todo.id} className={todo.done ? "done" : undefined}>
<label>
<input
type="checkbox"
data-toggle
checked={todo.done}
onChange={() => toggle(todo.id)}
/>{" "}
<span className="title">{todo.title}</span>
</label>
</li>
))}
</ul>
<div className="footer">
<span id="todo-count">{left} left</span>
<div className="filters">
{Object.entries(FILTERS).map(([name, label]) => (
<button
key={name}
type="button"
data-filter={name}
className={filter === name ? "selected" : undefined}
onClick={() => setFilter(name)}
>
{label}
</button>
))}
</div>
</div>
</main>
);
}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>`,
};
}