Genie React

Collapsing the sidebar re-rendered 1,292 components. One context value was the cause

One click on the sidebar toggle re-rendered every feature gate and priority picker on a 420-card board. Genie named the context value behind it, and verify proved the fix.

One click on Toggle sidebar re-rendered 1,292 components, and 1,260 of them were feature gates and priority pickers on board cards that did not change. The cause was FeatureFlagsProvider, which built a new context value on every render, so every component that read the flags rendered again whenever the app shell did.

This page walks through how a coding agent finds that with Genie and proves the fix. Every command and output below comes from Genie 1.1.0 runs against apps/realworld-legacy in this repository, a React 18.3 project tracker built with webpack, Redux Toolkit and connect(). The raw evidence has every command with its full output.

The symptom

The board shows 420 cards in five columns. Collapsing the sidebar changes one Redux field, ui.sidebarCollapsed. Only the shell and the sidebar should render.

Open a render window, click once, wait for React to go idle, and count:

npx genie-react call react_clear_renders '{"budget":{"adaptive":false,"fiberLimit":20000,"operationLimit":2000000,"timeLimitMs":500,"targetOperationReserve":100000,"targetTimeReserveMs":100}}' --session upd-c --select /ok
agent-browser --session upd-c click 'button[aria-label="Toggle sidebar"]'
npx genie-react call react_quiesce '{"idleMs":500,"timeoutMs":10000}' --fail-on-result-error --session upd-c --select /outcome
npx genie-react call react_get_renders '{"appOnly":false,"limit":1}' --session upd-c --select /summary

The budget is the one the repository's eval probes use for this app. The summary, trimmed to the counts:

result:
  commits: 3
  trackedComponents: 1288
  totalRenders: 1292
  totalUpdates: 1292
  semantics: exact

The same read with "component":"FeatureGate" returned totalUpdates 840, and with "component":"PriorityPicker" it returned 420. That is two gates and one picker on every card.

The evidence

react_render_causes says why one of those gates rendered:

npx genie-react call react_render_causes '{"component":"FeatureGate","limit":1}' --session upd-c
FeatureGate #9355814510355 {renderEventId:render:27:1292,…,necessity:unnecessary,necessityReason:output-unchanged,…}
  ← context exact "Context(FeatureFlagsProvider)" {contextIndex:0}

The cause is a context change, graded exact, and the render produced the same output it had before. Add "view":"full" to see what changed inside the context value. This is the cause object from that read, trimmed with …:

causes:[{kind:context,evidence:exact,…,name:"Context(FeatureFlagsProvider)",provider:{componentId:12257501118922,componentName:FeatureFlagsProvider},before:{flags:{card-estimates:true,card-labels:true,inline-priority:true,reports-workload:true,issue-presence:true},isEnabled:{…preview:"ƒ isEnabled()",…}},after:{flags:{card-estimates:true,card-labels:true,inline-priority:true,reports-workload:true,issue-presence:true},isEnabled:{…preview:"ƒ isEnabled()",…}},deepDiff:{changes:[{kind:identity-changed-uninspected,path:/isEnabled}],visited:3,truncated:true}}]

The five flags are identical before and after. The only change is /isEnabled, a function with a new identity.

An unfiltered react_get_renders read after one toggle on a freshly loaded board shows where the cascade started. Trimmed:

AppShell #12257501118921 {…,wrappedBy:{…,componentName:"Connect(AppShell)",…,causes:[{kind:external-store,evidence:inferred,name:selector,changedFields:[sidebarCollapsed],redux:{evidence:inferred,actions:[ui/sidebarToggled]}}],…},…}
  ← parent inferred "Connect(AppShell)"
  ← props exact sidebarCollapsed
FeatureFlagsProvider #12257501118922 {…}
  ← parent inferred AppShell {origin:"Connect(AppShell)"}
  ← children exact

So the chain is ui/sidebarToggled, then Connect(AppShell), then AppShell with a new sidebarCollapsed prop, then FeatureFlagsProvider, then every flag consumer.

The cause

AppShell has to render when the sidebar collapses. The problem is what its child does when it renders:

src/contexts/feature-flags.tsx
export function FeatureFlagsProvider({ flags, children }: FeatureFlagsProviderProps) {
  const value = { flags, isEnabled: (name: string) => flags[name] === true }
  return <FeatureFlagsContext.Provider value={value}>{children}</FeatureFlagsContext.Provider>
}

Each render creates a new object and a new isEnabled arrow. React compares a provider's value with Object.is, sees a new object, and renders every component that calls useContext on it. BoardCard is wrapped in memo and its props did not change, but a context update reaches FeatureGate and PriorityPicker without passing through that check.

The fix

Keep the value stable while flags is stable:

-import { createContext, type ReactNode, useContext } from 'react'
+import { createContext, type ReactNode, useContext, useMemo } from 'react'
@@
 export function FeatureFlagsProvider({ flags, children }: FeatureFlagsProviderProps) {
-  const value = { flags, isEnabled: (name: string) => flags[name] === true }
+  const value = useMemo(() => ({ flags, isEnabled: (name: string) => flags[name] === true }), [flags])
   return <FeatureFlagsContext.Provider value={value}>{children}</FeatureFlagsContext.Provider>
 }

flags comes from config?.flags ?? NO_FLAGS in AppShell. RTK Query returns the same config object until it refetches, and NO_FLAGS is a module constant, so the memo holds across sidebar toggles.

The proof

Declare what the fix must make true, click, and let Genie decide. render SideNav min 1 is the positive control. If the click never reached the app, that claim fails.

npx genie-react verify begin --session upd-c --name 'toggle the sidebar' \
  --claim 'render FeatureGate max 0' --claim 'render SideNav min 1' --claim 'boundaries clean' \
  --budget '{"adaptive":false,"fiberLimit":20000,"operationLimit":2000000,"timeLimitMs":500,"targetOperationReserve":100000,"targetTimeReserveMs":100}'
agent-browser --session upd-c click 'button[aria-label="Toggle sidebar"]'
npx genie-react verify end vfy_7341a0a0-30eb-4821-9349-8bd6a37abd6e --session upd-c

Before the fix, trimmed to the verdict, the claim lines and the exit code:

fail vfy_7341a0a0-30eb-4821-9349-8bd6a37abd6e "toggle the sidebar"  4 commits, quiet after 657 ms (react)
  1 fail render FeatureGate max 0  {renders:840,mounts:0,updates:840,remounts:0,semantics:exact,instances:840,activeInstances:840}
  2 pass render SideNav min 1  {renders:1,mounts:0,updates:1,remounts:0,semantics:exact,instances:1,activeInstances:1}
  3 pass boundaries clean  {caughtDuringWindow:0,caughtAtEnd:0}
exitCode: 1

Genie resolved all 840 FeatureGate instances and counted 840 renders, graded exact. That matches react_get_renders above. The budget carries the same target reserve as the symptom read. Without targetOperationReserve, the same claim counted 37 renders, marked lower-bound. That still fails max 0, because any render breaks an upper bound, but only exact evidence can pass it. Genie 1.0.2 read only 200 of the 840 instances here, and 1.1.0 reads up to 2,000.

With the fix applied and the page reloaded, the same begin, click and end, with the new ID, printed:

pass vfy_5ee04ad1-30e5-461e-b1a4-236c461c67cf "toggle the sidebar"  4 commits, quiet after 786 ms (react)
  1 pass render FeatureGate max 0  {renders:0,mounts:0,updates:0,remounts:0,semantics:exact,instances:840,activeInstances:840}
  2 pass render SideNav min 1  {renders:1,mounts:0,updates:1,remounts:0,semantics:exact,instances:1,activeInstances:1}
  3 pass boundaries clean  {caughtDuringWindow:0,caughtAtEnd:0}
exitCode: 0

The update counts from the symptom section, repeated on the fixed code:

ComponentUpdates beforeUpdates after
All components1,29233
FeatureGate8400
PriorityPicker4200
BoardColumn55

When this approach does not fit

The five BoardColumn renders survive the fix. They come from a reselect selector shared by all five columns, a separate bug covered in useSelector returned a new object.

Genie counted renders here, not time. These runs did not measure how long the click took, so this page makes no claim about frame time. To prove a speed change, compare repeated captures as Optimize describes.

useMemo only helps when its inputs are stable. If AppShell passed a new flags object on every render, the memo would rebuild each time, and the fix would belong where flags is created.

Genie does not look at the screen. It reports what React did, so confirm the sidebar still collapses with your browser driver. Verify a fix lists every claim form, and Debug renders and effects covers the render tools used here.

On this page