Contents Lesson 6 of 19

9 min read · professional

A click here changes something there

By the end of this lesson you will have a selection owned by the container, a quote panel that follows it and a watchlist that stays still, the selected symbol in the page address and checked before use — and a feedback loop you created on purpose, watched, and closed.

This is the feature that turns four panels into a cockpit: click a row in the screener and the chart, the quote and the backtest all follow.

One shared concept, owned by the container

type Selection = { symbol: string | null; source: string };

One field for what, one for which panel said so. The container owns it; panels receive it and call onSelect.

The source field looks like decoration and is not. Without it a panel cannot tell whether the selection came from itself, so it cannot avoid reacting to its own click — and that is the loop this pattern is famous for.

The loop, concretely

Panel A calls onSelect("AAPL.US"). The container updates selection. Panel A re-renders, sees a new selection, and calls onSelect again. If anything in that path is asynchronous or debounced, you now have a cockpit that fetches forever and a meter that climbs while nobody touches the page.

Two rules stop it, and you want both:

Panels report events, they do not react to their own. Check source before responding.

onSelect with the current value is a no-op. Compare before setting state. One line in the container, and it makes the whole system tolerant of panels that get it wrong.

Not everything should follow

The tempting version is that every panel tracks the selection. Resist it, because a cockpit where everything moves at once is one you cannot read.

Let each panel declare its own behaviour: follows: true for a chart or a quote, false for a watchlist you deliberately keep still. Put that in the panel's config, so it is saved with the layout and the person decides — you will want the same panel following in one layout and pinned in another.

Selection is not persisted

It belongs in the shared tier from unit 1, not the persisted one. Reloading the page should not restore what you happened to be looking at yesterday; it should restore your layout.

The exception is the URL. Putting the selection in the query string makes a view shareable and survives a refresh without going near the database — and it is the same "URL is untrusted input" rule from Build a Screener, so validate the symbol against your allowlist before anything touches it.

Try it now

Wire selection between two panels with the prompt below. Then break it, on purpose, and see what the guard was for.

The prompt

Mock mode on, plan mode on, and paste:

Plan, then build, selection for the dashboard. The container in
src/components/Cockpit.tsx owns one Selection { symbol, source } and a
select(symbol, source) that does nothing when that symbol is already
selected. Panels receive the selection and report clicks through
onSelect. Add a quote panel, src/components/panels/QuotePanel.tsx, to
the default layout, showing one symbol with its price and its age. Add follows to each panel's config, saved with the
layout: true for the quote panel, false for the watchlist. The container
decides what a following panel shows, and never drives a panel with a
selection that came from that panel. Mirror the symbol into the address
as ?symbol=, and accept it on the way in only if the page already shows
that symbol: read it back in an effect that runs again whenever what the
page shows changes, from window.location rather than the render's
searchParams. Tell me with estimateCalls what selecting a symbol costs;
one already on the watchlist must cost nothing. Write
tests/selection.test.ts first, for the no-op and the address check.
Mock mode. Run npm test, typecheck, lint and check:leak, and show me
the diff.

A good answer adds a quote panel, a selection module and its test, and changes the container and the panels' configs in the layout module. No panel imports another.

Check it yourself

  1. Click a row in the watchlist panel: the quote panel shows that symbol, and the watchlist panel does not move.
  2. The address now ends in ?symbol= and the symbol; reload and the same symbol is still selected.
  3. Change the address to a symbol no panel is showing (Coca-Cola's, KO with the US suffix, is on none of the defaults) and reload: nothing is selected, and the Network tab shows no request for it.
  4. grep -n 'Panel"' src/components/panels/*Panel.tsx prints nothing: no panel file imports another panel.

Break it on purpose

The prompt asked for two guards, and the loop only shows when both are gone. First ask the agent to remove the no-op guard and click a row: nothing breaks, because the read-back effect only runs again when what the page shows changes, and that is the second guard. Now ask the agent to make the read-back effect run after every render, then click a row. The loop runs through the address: the selection is written to ?symbol=, the effect selects it again, and without the no-op guard every pass is a new selection, so the effect runs once more. In mock mode the fixtures cost nothing and the market store answers from its cache, so the meter and the Network tab stay still; what you see is a page that stops responding and React's "Maximum update depth exceeded" in the browser console. Live, with anything asynchronous in that path, the same loop is a meter that climbs while nobody touches the page. Put both guards back once you have seen it, and check git diff shows nothing left over.

Compare with ours
// src/lib/selection.ts: the container owns it, and forgives panels that get it wrong
export function nextSelection(current: Selection, symbol: string, source: string): Selection {
  return current.symbol === symbol ? current : { symbol, source };   // same value: no-op
}

// What a panel shows is the container's decision, from config, not the panel's code.
export function shownSymbol(panel: { id: string; config: { symbol?: string; follows: boolean } },
                            selection: Selection): string | null {
  if (!panel.config.follows || selection.source === panel.id) return panel.config.symbol ?? null;
  return selection.symbol ?? panel.config.symbol ?? null;   // nothing selected yet: its own symbol
}

// The address is a stranger's input: only a symbol already on the page is accepted.
export const fromAddress = (raw: string | null, showing: string[]) =>
  raw !== null && showing.includes(raw) ? raw : null;

Returning current itself, not a copy, is what lets React skip the render. The full state at the end of this unit is on the starter's branch ref/build-a-dashboard/u2.

Checkpoint

npm test && npm run typecheck
git status          # the files you expect, and no .env file
git add -A
git commit -m "Selection owned by the container: no-op on the same symbol, followers by config"

Then answer this lesson's questions.