Model loading, empty and error views
Make every important view explicit
A data-driven interface needs to distinguish waiting for a result, a successful result with no records, a failed request, and a successful result with records. An empty array alone does not tell you whether loading is finished. At this stage, pass a snapshot of the result as props; network requests and useState are later subjects. No request is made in these fixtures.
function ResultMessage({ phase }) { if (phase === 'loading') return <p role="status">Checking parcels...</p>; if (phase === 'error') return <p>Could not load parcels.</p>; if (phase === 'empty') return <p>No parcels to collect.</p>; if (phase === 'success') return <p>Parcels are available.</p>; return <p>Unknown result.</p>;}
export default function App() { return ( <main> <h1>Result examples</h1> <ResultMessage phase="loading" /> <ResultMessage phase="error" /> <ResultMessage phase="empty" /> <ResultMessage phase="success" /> </main> );}Expected: one paragraph for each of the four supplied phases. In a real single request there would be just one current phase. A role of status can announce updates politely when an existing live region changes; an announcement on initial insertion is not guaranteed. Actual assistive technology behavior needs a manual check. Error messages should say what went wrong without exposing raw service details.
Derive empty and populated views from successful data
function classifyResult(result) { if (result.status === 'loading') return 'loading'; if (result.status === 'error') return 'error'; if (result.status !== 'success') return 'unknown'; if (!Array.isArray(result.parcels)) return 'unknown'; return result.parcels.length === 0 ? 'empty' : 'populated';}
function ParcelResult({ result }) { const view = classifyResult(result); if (view === 'loading') return <p>Checking parcels...</p>; if (view === 'error') return <p>Could not load parcels. Please try again later.</p>; if (view === 'empty') return <p>No parcels to collect.</p>; if (view === 'populated') return <p>{result.parcels.length} parcels ready.</p>; return <p>Result unavailable.</p>;}
export default function App() { return ( <main> <ParcelResult result={{ status: 'loading' }} /> <ParcelResult result={{ status: 'error' }} /> <ParcelResult result={{ status: 'success', parcels: [] }} /> <ParcelResult result={{ status: 'success', parcels: ['P1', 'P2'] }} /> </main> );}Expected: checking, error, empty and 2 parcels ready messages. The contract is a result object; only success needs a parcels array. A single status avoids combinations like loading and error both being true. Empty is derived from the successful array, not stored as another potentially contradictory flag. Array.isArray guards the container shape but does not validate every parcel record; real external data must be checked at its boundary. Rendering the actual records comes in the next chapter.
Keep surrounding UI consistent
function ParcelMessage({ result }) { if (result.status === 'loading') return 'Checking parcels...'; if (result.status === 'error') return 'Could not load parcels.'; if (result.status !== 'success') return 'Result unavailable.'; if (!Array.isArray(result.parcels)) return 'Result unavailable.'; return result.parcels.length === 0 ? 'No parcels to collect.' : `${result.parcels.length} parcels ready.`;}
export default function App() { const result = { status: 'success', parcels: [] }; return ( <main> <h1>Collection desk</h1> <p role="status"><ParcelMessage result={result} /></p> </main> );}Expected: Collection desk followed by No parcels to collect. Change the fixed result object and rerun the example to inspect another view. The heading and message container stay structurally consistent across alternatives. In an interactive app, keeping a live region present and updating its text can help announce status changes; test that with a real browser and assistive technology. There is no retry button here because no retry action is implemented. An error message is not a React Error Boundary, and this example does not catch render exceptions.
Practice
Design a result view for a library reservation. Accept loading, error, or success with an array. Distinguish empty and populated success, handle an unexpected status, and keep a heading visible in every case. Write a table of inputs and predicted output before using the browser.