← Back to React, page 8

react-0148 · Hard senior

What is islands architecture and partial hydration, and how does it differ from hydrating a whole React page?

With full hydration, the client downloads and runs the JavaScript for every component on the page, even static ones, to attach event handlers. Islands architecture renders the page to static HTML and hydrates only the interactive components (the islands), often lazily when they become visible or idle. The static parts ship no JavaScript. Astro popularised it, and React Server Components reach a similar goal by keeping server components out of the client bundle.

Full answer

Hydration attaches event handlers and state to server-rendered HTML. In a classic React SSR app (hydrateRoot(document, <App />)), the whole component tree must be loaded and executed in the browser, including components that never change, such as headers, articles and footers. The cost is JavaScript download, parse and execution, which delays interactivity (INP) on slower devices.

Islands architecture treats a page as mostly static HTML with a few interactive islands. The server renders everything. The browser receives HTML, plus a small script for each island, with its props serialized in the markup. Each island is hydrated independently with hydrateRoot on its own container, and the page can decide when: immediately (client:load in Astro), when the browser is idle, when the island scrolls into view (client:visible), or on a media query.

Benefits: much less JavaScript for content sites, faster interactivity, and independent failure and loading of islands. Costs: no shared React tree across islands, so shared state needs a store outside React or events, no context across islands, props must be serializable, and the mental model differs from a single app. It suits content-heavy pages such as blogs, documentation and marketing sites, and not highly interactive dashboards.

Related approaches:

  • Selective hydration in React 18+ (Suspense boundaries hydrate independently and prioritise what the user interacts with, but all code is still shipped).
  • React Server Components, where server components never ship to the browser and only use client components are hydrated.
  • Resumability (Qwik), which avoids hydration by serializing the state and loading handlers on demand.

The example simulates islands: it renders static HTML on the server with two island placeholders that carry serialized props, then hydrates one island and leaves the other untouched. It uses jsdom and hydrateRoot for real.

jsx
import { act, useState } from 'react';
import { hydrateRoot } from 'react-dom/client';
import { renderToString } from 'react-dom/server';

// Lets act() flush updates in this demo script. Browsers do not need this.
globalThis.IS_REACT_ACT_ENVIRONMENT = true;

function Counter({ start }) {
  const [count, setCount] = useState(start);
  return <button onClick={() => setCount(count + 1)}>Count {count}</button>;
}


const html = '<main><h1>Static article</h1>'
  + '<div id="island-1" data-props=\'{"start":1}\'>' + renderToString(<Counter start={1} />) + '</div>'
  + '<div id="island-2" data-props=\'{"start":50}\'>' + renderToString(<Counter start={50} />) + '</div>'
  + '</main>';

document.body.innerHTML = html;
console.log('before hydration, island buttons have React props: ' + ['island-1', 'island-2'].map((id) => Object.keys(document.querySelector('#' + id + ' button')).some((k) => k.startsWith('__reactProps'))).join(', '));

const container = document.querySelector('#island-1');
await act(async () => {
  hydrateRoot(container, <Counter {...JSON.parse(container.dataset.props)} />);
});

console.log('after hydrating island 1 only: ' + ['island-1', 'island-2'].map((id) => Object.keys(document.querySelector('#' + id + ' button')).some((k) => k.startsWith('__reactProps'))).join(', '));

await act(async () => { document.querySelector('#island-1 button').dispatchEvent(new window.MouseEvent('click', { bubbles: true })); });
await act(async () => { document.querySelector('#island-2 button').dispatchEvent(new window.MouseEvent('click', { bubbles: true })); });
console.log('island 1 text: ' + document.querySelector('#island-1 button').textContent);
console.log('island 2 text (never hydrated, click ignored): ' + document.querySelector('#island-2 button').textContent);

This ran as a script in jsdom. Only the island that was hydrated responded to the click, while the other kept its static HTML. Output of the run: before hydration, island buttons have React props: false, false after hydrating island 1 only: true, false island 1 text: Count 2 island 2 text (never hydrated, click ignored): Count 50

Key points

  • Full hydration runs every component in the browser. Islands hydrate only the interactive parts.
  • Static parts ship no JavaScript, and islands can hydrate on load, idle or visibility.
  • Costs: no shared React tree or context across islands, and serializable props.
  • Selective hydration and Server Components are related but different tools.
  • It fits content sites better than highly interactive applications.

Common mistakes

  • Assuming SSR means the page is interactive immediately.
  • Sharing React context between separate islands.
  • Passing functions or class instances as island props.
  • Using islands for a highly interactive application.
  • Confusing selective hydration with shipping less JavaScript.

Follow-ups

  • How does React's selective hydration with Suspense differ from islands?
  • How would islands share state without a common React tree?
  • How do Server Components relate to islands?

Tips

  • Start with the cost of hydrating static markup.
  • Name the hydration strategies (load, idle, visible).
  • State the limits, especially shared state.