Use flushSync and resource hints only when justified
Synchronize immediately only at a real integration boundary
React normally schedules and batches updates. A host API may exceptionally require the DOM to reflect a state change before a callback returns. flushSync from react-dom runs the update with synchronous flushing; it is not a general performance optimization. It can force pending work or Effects to run and may reveal Suspense fallbacks. Do not call it during rendering or in an Effect. Prefer ordinary state updates and refs or Effects if the external contract allows them.
import React,{useRef,useState} from 'react';import {flushSync} from 'react-dom';function hostMeasure(list){return list.children.length;}export default function App(){const [rows,setRows]=useState(['Print']);const [reported,setReported]=useState(null);const list=useRef(null);function add(){flushSync(()=>setRows(old=>[...old,`Room ${old.length+1}`]));setReported(hostMeasure(list.current));}return <><button type="button" onClick={add}>Add and measure</button><ul ref={list}>{rows.map(row=><li key={row}>{row}</li>)}</ul><p role="status">Host row count: {reported??'Not measured'}</p></>;}The hostMeasure function stands in for a synchronous external contract. After the first click the committed list has two children; the recorded count is 2. Without flushSync the immediate DOM read may see the previous list. In an ordinary React-only UI, derive the count from rows.length and remove the flushSync call. Avoid measuring layout or forcing synchronous work for every keystroke.
Choose resource hints by the work they initiate
import React,{useState} from 'react';import {preload} from 'react-dom';const image='/gallery-map.svg';export default function App(){const [visible,setVisible]=useState(false);return <section><button type="button" onFocus={()=>preload(image,{as:'image'})} onPointerEnter={()=>preload(image,{as:'image'})} onClick={()=>setVisible(true)}>Show gallery map</button>{visible&&<img src={image} alt="Gallery map: entrance leads to the print and textile rooms" width={320} height={160}/>}</section>;}Copy the supplied gallery-map.svg fixture into the approved app public directory. Focusing or hovering over the control hints that an image may soon be needed; clicking renders the actual image. A hint is not a download guarantee, nor does it display the asset. Browsers can prioritize differently. Measure network timing and avoid preloading every asset. React 19 resource helpers: prefetchDNS resolves an anticipated hostname; preconnect prepares a connection to an origin; preload fetches a known resource; preloadModule fetches an ES module without requesting immediate execution; preinit requests script execution or stylesheet application; preinitModule requests module execution. These last two are not harmless speculative fetches. Use approved local resources, compatible fetch/credentials options and framework-managed loading where possible. Never preinit unknown scripts. The examples do not contact external services.
Use DOM props, metadata and custom elements deliberately
import React,{useState} from 'react';class RoomBadge extends HTMLElement{set room(value){this.textContent=value.name;}}if(!customElements.get('museum-room-badge'))customElements.define('museum-room-badge',RoomBadge);export default function App(){const [name,setName]=useState('Print');return <main className="gallery" style={{padding:12}}><title>Gallery directory</title><meta name="description" content="Local gallery room directory"/><h1>Choose a room</h1><label>Room <select value={name} onChange={e=>setName(e.target.value)}><option>Print</option><option>Textile</option></select></label><p data-room={name}><museum-room-badge room={{name}}/></p></main>;}React 19 can assign an object to a custom-element property when the property exists on the element instance. This example registers the element before rendering and owns its internal text; React does not render children into it. Expect Print, then Textile after selection. Custom events may be handled using the exact event name and casing in React 19; older versions may require a ref and native listener with cleanup. A native HTML element uses className, camel-cased style keys and event props, but aria-* and data-* retain hyphens. Controlled select uses value and onChange. React 19 supports special metadata and resource behavior for title, meta, link, style and script under documented conditions; for example, the title belongs in head. Coordinate with the framework, and avoid duplicate document titles. Stylesheet precedence and async scripts have additional rules, not all resource tags are automatically hoisted or deduplicated. The custom element is browser-only and its upgrade behavior is not tested here. The evolving React DOM index also lists browser for server-rendering boundaries; defer it to the versioned server rendering lane, and verify its channel before use. Practice: replace the React-only count with a derived value, inspect the local image request and head metadata, and explain why preloading is not preinitializing.