Migrate removed root APIs and legacy refs
Migrate behavior, not just syntax
React 19 removed several older React DOM and class interfaces. Audit the installed application and test existing behavior before upgrading. React 18 already supports createRoot and hydrateRoot; migrate old render, hydrate and unmountComponentAtNode calls to root APIs. Preserve the distinction between mounting a client-only app and hydrating matching server HTML. Replace findDOMNode with a direct DOM ref, string refs with createRef or callback refs, and old contextTypes/getChildContext with createContext and contextType or useContext. Do not treat still-supported Component or PureComponent as removed.
import React,{Component,createRef} from 'react';import {createRoot} from 'react-dom/client';class SearchBox extends Component {input=createRef();render(){return <><label>Find tool <input ref={this.input}/></label><button type="button" onClick={()=>this.input.current?.focus()}>Focus search</button></>;}}export function mountSearch(host){const root=createRoot(host);root.render(<SearchBox/>);let mounted=true;return ()=>{if(mounted){mounted=false;root.unmount();}};}// Client host supplies empty container, saves dispose=mountSearch(host), and calls dispose() before removing host.This integration module is not a default App export. In the old code, ReactDOM.render(<SearchBox/>, host) and ReactDOM.unmountComponentAtNode(host) would manage mounting; those calls are historical and not runnable in React 19. A direct input ref replaces a string ref or findDOMNode lookup. The new root must be retained across updates. If the old app used ReactDOM.hydrate, use hydrateRoot(host, <SearchBox/>) instead, with matching server HTML. Legacy render callbacks require a behavioral redesign using a relevant ref callback or Effect, not a second root or a guessed timeout.
Compare class lifecycle with a function component contract
import React,{useEffect,useState} from 'react';const bus=new EventTarget();function RoomNotice({room}){const [count,setCount]=useState(0);useEffect(()=>{const event=`notice:${room}`;const receive=()=>setCount(n=>n+1);bus.addEventListener(event,receive);return ()=>bus.removeEventListener(event,receive);},[room]);return <p role="status">{room}: {count} notices</p>;}export default function App(){const [room,setRoom]=useState('Wood');const [visible,setVisible]=useState(true);return <><button type="button" onClick={()=>setRoom(r=>r==='Wood'?'Metal':'Wood')}>Switch room</button><button type="button" onClick={()=>bus.dispatchEvent(new Event(`notice:${room}`))}>Announce in current room</button><button type="button" onClick={()=>bus.dispatchEvent(new Event(`notice:${room==='Wood'?'Metal':'Wood'}`))}>Announce in other room</button><button type="button" onClick={()=>setVisible(v=>!v)}>Toggle notice</button>{visible&&<RoomNotice room={room}/>}</>;}The visible contract is similar to the class example: current-room events count, other-room events do not, switching rooms preserves the count, and unmount/remount resets it. The transport implementation is now a local EventTarget fixture; test the same visible behavior after Effects have settled. One Effect describes the resource lifetime, rather than mechanically mapping three lifecycle methods. Effect timing is not identical to all class lifecycle methods; if synchronous layout work is genuinely required, assess useLayoutEffect. Class getSnapshotBeforeUpdate has no direct Hook equivalent, and Error Boundaries still have no direct built-in function-component Hook equivalent. Retain a class boundary or use a separately reviewed library.
Preserve context, refs and rendering behavior
import React,{createContext,useContext,useRef,useState,memo} from 'react';const Workshop=createContext('General');const ToolCard=memo(function ToolCard({tool}){const room=useContext(Workshop);const input=useRef(null);return <section><h2>{room}: {tool.name}</h2><label>Tool note <input ref={input}/></label><button type="button" onClick={()=>input.current?.focus()}>Focus note</button></section>;});export default function App(){const [tool,setTool]=useState({name:'Clamp'});const [room,setRoom]=useState('Wood');return <Workshop.Provider value={room}><button type="button" onClick={()=>setTool(t=>({...t,name:t.name==='Clamp'?'File':'Clamp'}))}>Change tool</button><button type="button" onClick={()=>setRoom(r=>r==='Wood'?'Metal':'Wood')}>Change room</button><ToolCard tool={tool}/></Workshop.Provider>;}The function card preserves the visible heading and focus action from lesson one. memo can skip some prop-driven work but does not block context updates. Its props comparison is not a complete translation of PureComponent state comparison. Measure before adding optimizations. Replacing a class with a different component type in a running tree can reset its state; migration tests should start from a controlled initial state and verify user-visible state preservation where required. Before rollout: check dependencies and the React 19 upgrade guide, migrate roots/refs/context, remove unsafe lifecycle side effects, test fallbacks and subscriptions in StrictMode and production, and verify accessibility. Do not run codemods against an unreviewed repository. Practice: write a migration acceptance checklist for the room subscription and tool focus examples.