useSelector returned a new object, so the avatar menu re-rendered on every action
Typing five letters in the issue filter re-rendered the avatar menu nine times with identical data. Genie tied the render to the Redux action, and verify proved a two-line fix.
The avatar menu re-rendered after every store change because its useSelector call built a new
{ user, savedViewCount } object each time, and useSelector compares results with ===. Typing
cache into the issue filter re-rendered the menu nine times, while the user and the saved-view count
stayed the same.
Every command and output below comes from runs against apps/realworld-legacy in this repository, a
React 18.3 tracker with Redux Toolkit 1.9, react-redux 8 and RTK Query. The
raw evidence
has every command with its full output, including the attempts that did not work.
The symptom
AccountMenu sits in the top bar and shows the signed-in user's avatar. Typing in the issue filter
should not touch it. Open a render window, type, and count:
npx genie-react call react_clear_renders '{"component":"AccountMenu"}' --session upd-d --select /ok
agent-browser --session upd-d type 'input[aria-label="Filter issues"]' cache
npx genie-react call react_quiesce '{"idleMs":500,"timeoutMs":10000}' --fail-on-result-error --session upd-d --select /outcome
npx genie-react call react_get_renders '{"component":"AccountMenu","appOnly":false}' --session upd-d \
--fields name,renders,updates,causeCounts,necessity,necessityReason[1]:
- {name:AccountMenu,renders:9,updates:9,causeCounts:{external-store:9},necessity:unnecessary,necessityReason:inputs-unchanged}Nine renders for five keystrokes, all from an external store, all unnecessary because the inputs
did not change.
Link the render to the Redux action
The Genie skill's recipe for "which Redux action caused this render" is react_render_causes with
"view":"full", then redux_actions. Read both in one batch right after the interaction, so they
describe the same moment:
npx genie-react call react_clear_renders '{}' --session upd-d --select /ok
agent-browser --session upd-d type 'input[aria-label="View name"]' S
npx genie-react call react_quiesce '{"idleMs":500,"timeoutMs":10000}' --fail-on-result-error --session upd-d --select /outcome
npx genie-react batch '[{"tool":"react_render_causes","args":{"component":"AccountMenu","view":"full"}},{"tool":"redux_actions","args":{"typePrefix":"savedViews/","view":"full"}}]' \
--session upd-d --json > chain.jsonThis run types one character into the saved-view name box instead of the filter. The
last section explains why. The render cause, projected with jq,
trimmed with … and reflowed:
{
"renderEventId": "render:17:2",
"necessity": "unnecessary",
"necessityReason": "inputs-unchanged",
"cause": {
"kind": "external-store",
"evidence": "inferred",
"equality": "object-is",
"selectionEqual": false,
"changedFields": [],
"before": { "user": { "id": "u-1", "name": "Dana Whitfield", … }, "savedViewCount": 0 },
"after": { "user": { "id": "u-1", "name": "Dana Whitfield", … }, "savedViewCount": 0 },
"deepDiff": { "changes": [ { "kind": "reference-only", "path": "" } ], "visited": 3, "truncated": false },
"redux": {
"evidence": "exact",
"reason": "dispatched-between-renders",
"actions": [ { "sequence": 1960, "type": "savedViews/draftNameChanged", "changedSlices": [ "savedViews" ] } ]
}
}
}Read it from the bottom up. One action, savedViews/draftNameChanged, was dispatched between the
window opening and this render, so Genie grades the Redux link exact. The selector's result
before and after holds the same user and the same count of 0. deepDiff finds one change, at the
root path "", and it is reference-only. The comparison is object-is, so selectionEqual is
false and the menu renders.
This is the menu's first render in the window, with no earlier render to compare with. Genie still
grades the link exact because React had no pending work when the window opened. Genie 1.0.2
graded such a first render inferred with actions from before the window, and 1.1.0 fixed it.
The external-store cause itself stays inferred, because Genie sees the hook's result, not the
selector function. That is enough here. Equal values with a new root reference point straight at a
selector that builds a new object.
The same batch's redux_actions result, projected to a few fields and reflowed:
{
"sequence": 1960,
"type": "savedViews/draftNameChanged",
"changedSlices": [ "savedViews" ],
"payload": "S",
"renderEventIds": [ "render:17:2", "render:17:7", "render:17:26", "render:17:30" ]
}The action lists the menu's render, render:17:2, among the renders that named it as exact. It
changed savedViews.draftName. The menu reads savedViews.views.length, which did not change.
The cause
const { user, savedViewCount } = useAppSelector((state) => ({
user: state.session.user,
savedViewCount: state.savedViews.views.length,
}))react-redux runs every mounted selector after each dispatch and re-renders when the result is not
=== to the last one. This selector returns a new object literal every time, so every store change
re-renders the menu. The react-redux docs describe this exact case under
Equality Comparisons and Updates.
The fix
Select each value on its own, so each result is a stable reference or a primitive:
- const { user, savedViewCount } = useAppSelector((state) => ({
- user: state.session.user,
- savedViewCount: state.savedViews.views.length,
- }))
+ const user = useAppSelector((state) => state.session.user)
+ const savedViewCount = useAppSelector((state) => state.savedViews.views.length)Passing shallowEqual as the second argument also works. IssueTableToolbar in the same app does
that for its own object selector.
The proof
render IssueTableToolbar min 1 is the positive control. The toolbar must render while you type, so
a run that missed the input fails.
npx genie-react verify begin --session upd-d --name 'type cache in the issue filter' \
--claim 'render AccountMenu max 0' --claim 'render IssueTableToolbar min 1' --claim 'boundaries clean' \
--budget '{"adaptive":false,"fiberLimit":20000,"operationLimit":2000000,"timeLimitMs":500}'
agent-browser --session upd-d type 'input[aria-label="Filter issues"]' cache
npx genie-react verify end vfy_893fe945-b083-4b3a-af4d-d2dbef163247 --session upd-dBefore the fix, trimmed to the verdict, the claim lines and the exit code:
fail vfy_893fe945-b083-4b3a-af4d-d2dbef163247 "type cache in the issue filter" 11 commits, quiet after 814 ms (react)
1 fail render AccountMenu max 0 {renders:9,mounts:0,updates:9,remounts:0,semantics:exact,instances:1,activeInstances:1}
2 pass render IssueTableToolbar min 1 {renders:5,mounts:0,updates:5,remounts:0,semantics:exact,instances:1,activeInstances:1}
3 pass boundaries clean {caughtDuringWindow:0,caughtAtEnd:0}
exitCode: 1With the fix applied and the page reloaded, the same begin, type and end, with the new ID, printed:
pass vfy_c9a9cc76-b1fd-489e-ba81-baf26b7ee0a1 "type cache in the issue filter" 11 commits, quiet after 817 ms (react)
1 pass render AccountMenu max 0 {renders:0,mounts:0,updates:0,remounts:0,semantics:exact,instances:1,activeInstances:1}
2 pass render IssueTableToolbar min 1 {renders:5,mounts:0,updates:5,remounts:0,semantics:exact,instances:1,activeInstances:1}
3 pass boundaries clean {caughtDuringWindow:0,caughtAtEnd:0}
exitCode: 0The toolbar still renders five times, once per keystroke. The menu went from nine renders to zero.
Two more selectors with the same shape
The same app has two reselect selectors that hand components a new reference after unrelated actions. Each row below is one verification before and one after that fix, with only that fix applied.
| Interaction | Claim | Before | After |
|---|---|---|---|
| Toggle the sidebar on the board | render BoardColumn max 0 | fail, renders:5, exit 1 | pass, renders:0, exit 0 |
Type Sprint triage as a view name | render IssueTable max 0 | fail, renders:13, exit 1 | pass, renders:0, exit 0 |
selectCardsForColumn is one createSelector shared by five columns, each passing its own status.
Reselect caches one result, so each column overwrites the last one's entry and every column gets a
new array. The fix gives each column its own selector instance through a makeSelectCardsForColumn
factory and useMemo. Genie reports each column render as
external-store inferred anonymous-external-store, with ui/sidebarToggled as the only candidate
action. It cannot show the cache misses.
selectVisibleIssues takes an input selector that returns a new { filters, sort } object, so it
recomputes on every action. The fix splits it into two input selectors. Genie reports the class
IssueTable render as props exact rows with a reference-only diff (visited:101). It shows the
new rows array, not the work that built it.
In both cases Genie names the component, the changed reference and the action. You read the selector to find out why the reference changed.
When this approach does not fit
Genie keeps a bounded number of render events per window, and a busy page can push the one you want
out. A filter keystroke in this app changes which rows show, and each new row mounts a UserName
with its own RTK Query hook. In a one-keystroke filter run the menu rendered twice, and the window
had already dropped 39 render events (renderEventRetention.evictedEvents), including the menu's
first render. The render it kept came from an RTK Query cache update, linked inferred to an
unrecorded change in the service slice. The one-keystroke View name run above dropped none.
The Redux history is not the limit. In a five-keystroke filter run, redux_actions still held all
five issues/filtersChanged actions. It counted 4,949 RTK Query actions, thunks and cache updates
in coverage.libraryActions instead of keeping them as records. Genie 1.0.2 kept them in the
500-record history, where they pushed out the filter action, and 1.1.0 fixed it.
Read the cause on the smallest interaction that shows the bug, and check
renderEventRetention.evictedEvents before you treat a missing render event as proof of anything.
If your own actions overflow the history, coverage.evictedRecords grows. Pass a larger
historyLimit (up to 5000) to reduxCollector to keep more.
react-redux 8.1 and later also catch this bug in development, with a selector stability check that runs a selector twice on its first call. On this page the browser console showed:
[warning] Selector unknown returned a different result when called with the same parameters. This can lead to unnecessary rerenders.The warning comes from react-redux, not Genie, and Genie does not surface console messages. It does
not name the component, and by default it runs only on a selector's first call. Genie names the
component, counts its renders, links each one to an action, and proves the fix with
verify. The Redux tools page lists every
redux_actions field.
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.
Every keystroke fetched the activity feed. The dependency looked harmless
Typing 10 characters into a comment box sent 10 activity requests. Genie's effect audit named the inline array behind it, and verify proved the fix.