Workbench / The TiffinBox Build
S22 · V2·22
The Display in React
Volume 2, chapter 22. Stage 21 gave the kitchen display a pure core; stage 22 puts a second view on it, in React 19, and measures what that costs. The core gains a framework-free store (subscribe, getSnapshot, start, stop) that React reads through useSyncExternalStore and Angular will read through a signal. The first React version blocked for 143 ms on every 500-order snapshot; store updates are always synchronous, so startTransition can't help, and useDeferredValue moved the render but not the commit, and memo was defeated by a clock passed to every card. After progressive commits and narrower props, updates have no long tasks and the remaining cost is React itself: 61 KB gzipped and about 100 ms of start-up script on the throttled tablet. The same accessibility, focus and announcement checks as stage 20 pass on both views.
Stage 22: the display in React
The brief
What you'll build.
web-src/kitchen/store.ts: the display's state behindsubscribe,getSnapshot,apply,startandstop; it owns the EventSource, the retry timer and the silence watchdog, and keeps decisions in stage 21's pure modules; snapshots are immutable and replaced on change;web-src/react/:main.tsxmounts the app in<StrictMode>in place of the server-rendered list;KitchenApp.tsxreads the store withuseSyncExternalStore, renders the status line fromdescribe, and renders tickets as a memoisedTicketcomponent with pending state, retry notes, focus restore and announcements;- build: React 19.1.1 and esbuild 0.24 pinned in
package.json;npm run build:reacttype-checks with the strict settings from stage 16 and bundles toweb/react-kitchen.js; - server:
/kitchen/k<n>?view=reactserves the same page with the React bundle instead ofkitchen.js; - checks:
check-js.shbuilds and type-checks the React view;boundaries.mjsreads.tsx, keepsreact/a leaf (nothing imports it, it never imports the plain kitchen feature) and bansdangerouslySetInnerHTML;web-tests/stage22.pyruns stage 20's checks on the React view;stage18.pytakesVIEW=?view=reactand reports when all cards are on screen.
Why a real company needs this. Most product teams build UIs with a framework, and many inherit a mix: an old view and a new one, side by side, for months. The question is never "React or not" in the abstract; it's what a framework costs and buys on a particular screen, for particular users, measured. A display that already has a pure core makes that question cheap to answer, and keeps the business logic when the framework changes.
Why now. Chapter 22 is React: components, state as snapshots, effects, memoisation, external stores, rendering and concurrent features. Stage 21 separated the display's core from its view, so a React view is about 150 lines, and stage 20's tests say whether it's as good as the one it might replace.
What you'll have at the end.
- two views of the display, plain and React, on one store and one set of rules, passing the same accessibility and focus checks;
- a measured comparison of the two at 500 orders on a throttled CPU, with each optimisation step recorded;
- ADR-0024: when the build uses React, and how framework code is kept at the edge.
Theory to own
- External stores. Why does React need
useSyncExternalStorefor data it doesn't own, and what mustgetSnapshotguarantee? (Companion, Code walkthrough and drill D4.) - Render and commit. Which of React's work can be interrupted, and which can't? What does that mean for a 500-card update? (Companion, Gaps 2 and drill D2.)
- Effects and cleanup. Why does Strict Mode mount twice in development, and what must
startandstopdo so the display opens one stream? (Companion, drill D1.) - Memoisation. When does
memoskip a component, and what makes it stop skipping? (Companion, drill D3.) - The cost of a framework. What does React cost before the first render, and how do you decide whether it's worth it? (Companion, Gaps 3 and interview Q4.)
Build it
Step 1: a store any view can use
Write kitchen/store.ts. createKitchenStore(kitchen) keeps the open orders (a Map, as stage 21's orders.ts expects), the connection state and the server-time offset in one immutable KitchenState; publish(patch) replaces the state and notifies listeners. Move the EventSource adapter from kitchen.ts: snapshot, order and ping events update the state through applySnapshot, applyOrder and next; errors and silence go through the machine; entering backingOff closes the stream and starts the retry timer. Expose subscribe, getSnapshot, apply (for an order returned by a command), start and stop.
Checkpoint. check-js.sh passes; the module has no DOM access except EventSource, and no React import.
Step 2: a first React view, and what it costs
Add React and the types to package.json with exact versions, a web-src/react/tsconfig.json that extends the main one with "jsx": "react-jsx" and noEmit, and the build:react script (tsc, then esbuild with NODE_ENV set to production). Write main.tsx to replace the server-rendered list with a mount point, and KitchenApp.tsx: the store from useMemo, started and stopped in an effect, read with useSyncExternalStore; dish names from /menu.json; the status line from describe; one Ticket per open order, keyed by id. Add ?view=react to the server's kitchen page and VIEW to stage18.py, then profile both views at 500 orders.
Checkpoint. The React view goes live and shows orders; you have both views' numbers.
Step 3: make the updates cheap
(Wrapping the store's updates in startTransition does nothing: the Companion's Gaps 2 measures why, on a smaller page.) Render the list from useDeferredValue(state.orders), and measure. Then grow the rendered list by 50 cards per animation frame (a limit in state, raised in requestAnimationFrame until every order is shown), and measure. Finally, move the 15-second clock into the parent and pass each card its minutes and late instead of now, and measure.
Checkpoint. No long tasks during updates; the worst loading frame is no longer your code.
Step 4: the same behaviour as stage 20
In Ticket, port stage 20's interaction rules: aria-disabled while a command is in flight (not disabled, which drops focus); retry notes from postCommand; when the status changes and the card's buttons had focus, focus the new first button. Announcements go through the shared announcer from effects: new orders from state.lastChange, lateness when a card's late turns true. Write stage22.py: 60 orders, the React view goes live with 60 cards, Chromium's accessibility tree has 60 order headings, a new order appears, Enter on Accept keeps focus on the same card's next button, five orders in a second produce one announcement, and the console has no errors or warnings (which includes CSP violations).
Checkpoint. stage22.py prints the same results stage 20 does for the plain view.
Step 5: keep React at the edge
Add npm run build:react to check-js.sh so the React view is type-checked and bundled on every build. Extend boundaries.mjs: read .tsx files; fail if any module outside react/ imports from it, if react/ imports the plain kitchen.js feature, or if any file uses dangerouslySetInnerHTML. Plant each violation once and check the rule fires. Then run stages 12–20 and the server tests again.
Checkpoint. check-js.sh passes with 12 modules and 21 tests; both planted violations fail; earlier stages pass.
Senior and architect lens
1. Measure the framework, not just the components. React DevTools' profiler starts at the first render; the 100 ms of bundle evaluation before it only shows in the browser's profile and long-animation-frame attribution. Compare views on the target device, repeated, because single runs on shared hardware are noisy (one warm-up run here gave 161 ms).
2. ADR-0024: React in the build. Write it with these points:
- Decision: the plain view stays the default display; the React view is kept as an option (
?view=react) on the same store, as the reference for teams that will build screens in React. - Why: on the kitchen tablet the React view costs 61 KB gzipped and about 100 ms of start-up script, and buys nothing the display needs today; the shared store makes switching cheap if that changes (more screens, shared components, a React team).
- Rules: framework code lives only in
react/(and stage 23'sangular/); domain logic, adapters and formatting are framework-free; nodangerouslySetInnerHTML; effects that start anything return a cleanup. - Revisit when: a second screen needs the same components, or server rendering (stage 24) changes the start-up cost.
3. Store updates are urgent by construction. useSyncExternalStore gives consistency and gives up interruptibility. That's the right default for a display, where tearing would show two statuses for one order. Where interruptibility matters, copy into React state at the boundary (useDeferredValue), and keep commits small.
4. One store, many views. The store's interface is the narrow waist: subscribe, getSnapshot, commands. React, Angular and plain DOM each adapt to it in a few lines. It's the same move as stage 16's contract between server and client, inside the client.
5. Port the tests, not just the code. The React view was accepted because stage 20's checks pass on it: accessibility tree, focus, announcements, CSP. A rewrite without the old tests is a rewrite with new bugs.
What breaks first, and when it's addressed:
| Pressure | What breaks | Where it's addressed |
|---|---|---|
| React replaces the server-rendered first screen with an empty mount point | the 40 server-rendered cards disappear until the stream's snapshot arrives | stage 24 (server rendering and hydration) |
| two frameworks will read one store | each wiring duplicates subscribe and start/stop logic | stage 23 (the same store through Angular signals) |
| the plain view still has its own adapter code | two copies of the stream handling | stage 25 (the plain view moves onto store.ts) |
stage22.py checks behaviour, not appearance |
the views could drift visually | stage 26 (component screenshots for both) |
| the bundle is one 193 KB file | every view pays for all of React | stage 27 (performance budgets) and stage 31 (hashed, cached assets) |
Evidence
check-js.shpasses: contract, types, the React build, architecture rules (12 modules), 21 tests; both planted violations are in the README.stage22.pypasses: 60 headings in the tree, focus kept, one batched announcement, no console errors; bundle sizes recorded.- The stage 18 profile for each step of this stage (first version,
useDeferredValue, progressive limit, narrow props) and three final runs of both views are recorded. - Stages 12–20 and the 28 server tests still pass.
- ADR-0024 is committed.
Carry forward
kitchen/store.tsis the display's state for every view: stage 23 reads it through Angular signals, and stage 25 moves the plain view onto it.?view=reactstays as a reference view; stage 26 adds component-level tests for it.- The boundary rules now cover
.tsx; stage 30 turns folders into packages with enforced dependencies. - The progressive limit is how any view renders large lists; stage 24's server rendering decides what the first screen is.