Skip to content

fix(app): call SessionsList hooks before the early return (Rules of Hooks)#1426

Open
chphch wants to merge 1 commit into
slopus:mainfrom
chphch:fix/sessionslist-hook-order
Open

fix(app): call SessionsList hooks before the early return (Rules of Hooks)#1426
chphch wants to merge 1 commit into
slopus:mainfrom
chphch:fix/sessionslist-hook-order

Conversation

@chphch

@chphch chphch commented Jun 21, 2026

Copy link
Copy Markdown
Contributor

SessionsList defines three useCallback hooks (keyExtractor, renderItem, HeaderComponent) after its early if (!data) return, which violates the Rules of Hooks: when a mounted instance's data (the memoized sessionListViewData) flips from null to a populated array, the render goes from fewer hooks to more and React throws Rendered more hooks than during the previous render, dropping the whole screen to the error boundary. In normal use the store has already settled sessionListViewData to [] (non-null) before the component mounts, so the early-return path is effectively dead and the bug stays latent — but any code path that mounts the list while the store is still empty and then populates it hits the crash. This moves the early return below all hooks so the hook order is identical on every render, and null-guards renderItem's data lookups (it's now typed nullable above the narrowing, though FlatList only invokes it once data is non-null). No behavior change otherwise.

Proof

Reproduced on Expo web by mounting <SessionsList/> while sessionListViewData === null, then injecting sessions on the next tick (the null → array transition). Left = current main (error boundary: "Rendered more hooks than during the previous render"); right = with this fix (the list renders normally).

SessionsList hook-order before/after

pnpm typecheck is clean.

…ooks)

SessionsList defined three useCallback hooks (keyExtractor, renderItem,
HeaderComponent) *after* an early `if (!data) return`. When a mounted
instance's session list data flips from null to a populated array, the
render goes from fewer hooks to more, throwing "Rendered more hooks than
during the previous render" and dropping the whole screen to the error
boundary.

Move the early return below all hooks so the hook order is identical on
every render, and null-guard renderItem's `data` lookups (it is now typed
nullable above the narrowing, though FlatList only invokes it once data is
non-null).

Generated with [Claude Code](https://claude.ai/code)
via [Happy](https://happy.engineering)

Co-Authored-By: Claude <noreply@anthropic.com>
Co-Authored-By: Happy <yesreply@happy.engineering>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant