Reviewer · React
Scope state to entity identity
Local component state (loading/retry flags, cached async results, selected tab/page/sort, stacked panels) must stay tied to the entity it was computed for. When a component is reused for a different row, session, or record without remounting, stale state from the previous entity otherwise leaks through: a retry spinner that never clears, a cached thumbnail...
Local component state (loading/retry flags, cached async results, selected tab/page/sort, stacked panels) must stay tied to the entity it was computed for. When a component is reused for a different row, session, or record without remounting, stale state from the previous entity otherwise leaks through: a retry spinner that never clears, a cached thumbnail shown for the wrong session, a runbook flyout pinned to the alert it was opened for instead of following the one now in view, or a table page/sort computed for a now-stale set of errors.
Guard against this in one of two ways:
- Key cached or derived state by the entity’s identifier and only use it when the key matches the current entity (e.g.
const sessionKey = \${serviceName}:${sessionId}`; const snapshot = cached?.key === sessionKey ? cached.snapshot : undefined;`), so a stale value is simply ignored rather than shown. - Explicitly reset local state in a
useEffectkeyed on the identity, e.g.useEffect(() => { setStackedFlyout(undefined); }, [alertId]);, or key the component itself (e.g. bysymptom/version) so React remounts it and starts fresh.
When introducing such state, ask explicitly: “what happens if the surrounding identity changes while this state is set?” and add a test that switches entities mid-flight (e.g. re-render while a request is in progress, or select a different row before a save completes) to catch regressions.