Load code with lazy and Suspense
Load a component only when needed
Suspense sets a loading view for part of a React tree that is not yet ready to render. Use lazy to delay loading a component module until its first attempted render. The loader must resolve to an object whose default property is a valid component. In the first example, save the separate ExhibitPanel.jsx fixture alongside App.jsx. Use a matching approved React and React DOM 18+ client environment and a bundler that handles dynamic imports. The visual outcomes in this chapter are expected, not observed.
import React,{Suspense,lazy,useState,startTransition} from 'react';const Panel=lazy(()=>import('./ExhibitPanel.jsx'));export default function App(){const [open,setOpen]=useState(false); return <section><button onClick={()=>setOpen(v=>!v)} aria-expanded={open} aria-controls="exhibit-panel">{open?'Close':'Open'} exhibit</button><div id="exhibit-panel">{open&&<Suspense fallback={<p role="status">Loading exhibit…</p>}><Panel/></Suspense>}</div></section>;}Before opening, the panel is absent. On first opening, the loader starts and the boundary can display its status until the module is ready. The panel then shows a cedar specimen. React retains the loader result for this lazy component identity; closing and reopening does not normally reload it. Its local component state can still reset when removed from the tree. Declare lazy at module scope to avoid replacing its identity on each render. A fast load may make the fallback imperceptible. Actual output chunking depends on the bundler.
Choose which sections reveal independently
import React,{Suspense,lazy,useState,startTransition} from 'react';function controlledPromise(){ let resolve,reject; const promise=new Promise((yes,no)=>{resolve=yes;reject=no;}); // The fixture owns the promise; prevent unhandled rejection before mounting. promise.catch(()=>{}); return {promise,resolve,reject};}const overview=controlledPromise(), detail=controlledPromise();const Overview=lazy(()=>overview.promise), Detail=lazy(()=>detail.promise);export default function App(){return <section><button onClick={()=>overview.resolve({default:()=> <p>Exhibit overview</p>})}>Release overview</button><button onClick={()=>detail.resolve({default:()=> <p>Cedar details</p>})}>Release details</button><Suspense fallback={<p role="status">Waiting for overview</p>}><Overview/><Suspense fallback={<p role="status">Waiting for details</p>}><Detail/></Suspense></Suspense></section>;}This single-run fixture uses synthetic promises, not actual code splitting. Release the overview first: its text can appear while the inner section is still waiting. Release the details and both sections become available. Reload the page for a fresh run. The inner boundary limits how much ready UI must wait. A component that suspends before its first successful mount has no committed state to preserve. An Effect that starts a request after commit does not automatically activate a Suspense boundary; ordinary fetching still needs explicit loading UI or a supported integration.
Keep revealed content during a non-urgent navigation
import React,{Suspense,lazy,useState,startTransition} from 'react';function controlledPromise(){ let resolve,reject; const promise=new Promise((yes,no)=>{resolve=yes;reject=no;}); // The fixture owns the promise; prevent unhandled rejection before mounting. promise.catch(()=>{}); return {promise,resolve,reject};}const detail=controlledPromise();const Detail=lazy(()=>detail.promise);export default function App(){const [page,setPage]=useState('overview');return <section><button onClick={()=>startTransition(()=>setPage('detail'))}>Visit details</button><button onClick={()=>detail.resolve({default:()=> <p>Cedar details</p>})}>Release details</button><Suspense fallback={<p role="status">Loading details</p>}>{page==='overview'?<p>Overview ready</p>:<Detail/>}</Suspense></section>;}After the overview has committed, visit details before releasing the promise. React can keep the previously revealed boundary visible during the transition rather than replacing it with loading text. After release the detail appears. A newly mounted nested boundary may still show its own fallback. Transitions do not guarantee that all loading indicators disappear. A rejected lazy loader needs an Error Boundary, not another Suspense boundary; resetting only the boundary does not clear the cached rejection of the same lazy identity. In a real deployment handle failed chunks with a reviewed recovery policy. Practice: compare urgent navigation to the transition and explain which content remains usable.