Mount and unmount React DOM roots
A root is a boundary of React ownership
React DOM connects React components to the browser. A root owns the children of a particular DOM container. The browser provides document, element and focus APIs; React DOM provides mounting, reconciliation and integration APIs. In a client-only app, createRoot comes from react-dom/client. Use a single root for a typical app, and additional roots only for independent islands embedded in a host page. These examples assume matching, patched React and React DOM 19.x; createRoot and hydrateRoot also exist in React 18. No dependencies were installed in the authoring environment.
import React, {useState} from 'react';import {createRoot} from 'react-dom/client';function Notice({room}) {const [expanded,setExpanded]=useState(false);return <section><h2>{room}</h2><button type="button" aria-expanded={expanded} onClick={()=>setExpanded(v=>!v)}>Hours</button>{expanded&&<p>Open 10–16</p>}</section>;}const host=document.getElementById('root');if(!host) throw new Error('Missing widget container');const root=createRoot(host);root.render(<Notice room="Print gallery"/>);// An external host can later call root.render(<Notice room="Textile gallery"/>).Use this as the entry module in the Chapter 3 shell. Initially you see Print gallery and an Hours button. Clicking reveals opening hours. A subsequent render of Notice in the same position can preserve its expanded state. Do not create a new root on each update. The first render replaces pre-existing children in a client-only container; it does not attach to server-rendered React markup. Code immediately following render is not a reliable indication that the DOM has committed.
Unmount before the host removes its container
import React,{useEffect} from 'react';import {createRoot} from 'react-dom/client';function Widget(){useEffect(()=>{const listener=()=>console.info('Widget event');window.addEventListener('museum-refresh',listener);return ()=>window.removeEventListener('museum-refresh',listener);},[]);return <p>Gallery information ready</p>;}export function mountWidget(host){const root=createRoot(host);root.render(<Widget/>);let active=true;return ()=>{if(active){active=false;root.unmount();}};}// Host integration: const dispose=mountWidget(container); dispose(); container.remove();This is an integration module, not an App export. The host supplies an existing empty container and calls the returned disposer before removing it. Cleanup disconnects the listener; repeated disposal is harmless in this wrapper. Do not call render on the disposed root. A new root may be created for that container if it remains available. In development StrictMode, setup and cleanup can be exercised more than once. Do not externally replace children while React owns them.
Hydrate existing React HTML instead of replacing it
import React,{useState} from 'react';import {hydrateRoot} from 'react-dom/client';export function Counter(){const [count,setCount]=useState(0);return <button type="button" onClick={()=>setCount(n=>n+1)}>Visits: {count}</button>;}export function attach(host){return hydrateRoot(host,<Counter/>,{onRecoverableError(){console.warn('Hydration recovered');},onCaughtError(){console.warn('Boundary caught an error');},onUncaughtError(){console.error('Root error');}});}// Server fixture: renderToString(<Counter/>) placed inside host; call attach(host) on client.This is a partial two-environment integration example. The server must use the same Counter definition and initial value, with react-dom/server renderToString (or a suitable streaming renderer); the client must receive the matching markup before attach. Expect Visits: 0 to remain visible and increment when activated. Do not hydrate arbitrary HTML or use different initial timestamps or locale-dependent strings. Hydration mismatches are bugs to investigate, not warnings to silence. React 19 adds caught and uncaught error callbacks to the root options; React 18 supports onRecoverableError. Error reporting is not an Error Boundary. When using multiple roots and useId with SSR, coordinate identifierPrefix between server and client. Legacy render, hydrate and unmountComponentAtNode were removed in React 19; migrate to the corresponding root operations. Practice: explain why mounting and hydration are different, and identify who is responsible for disposal.