Skip to content

fix: move useEffect hooks before early return in ChatProfiles#2888

Closed
EyalAmitay wants to merge 1 commit intoChainlit:mainfrom
EyalAmitay:EyalAmitay/fix-hooks-violation
Closed

fix: move useEffect hooks before early return in ChatProfiles#2888
EyalAmitay wants to merge 1 commit intoChainlit:mainfrom
EyalAmitay:EyalAmitay/fix-hooks-violation

Conversation

@EyalAmitay
Copy link
Copy Markdown
Contributor

@EyalAmitay EyalAmitay commented Apr 9, 2026

Summary

  • Fix React Rules of Hooks violation in ChatProfiles.tsx where two useEffect hooks were placed after an early return
  • When config loads asynchronously (undefined → populated), the hook count changes between renders (8 → 10), causing React to throw "Rendered more hooks than during the previous render"
  • Move the early return below both useEffect calls and add optional chaining guards so they no-op when config.chatProfiles is missing

Test plan

  • pnpm type-check passes
  • pnpm lint passes
  • pnpm test (frontend unit tests) — 32/32 pass
  • Manual: verify chat profiles load correctly when config has 2+ profiles
  • Manual: verify component returns null gracefully when 0-1 profiles configured

Summary by cubic

Fixes a React Rules of Hooks violation in ChatProfiles by moving both useEffect hooks above the early return so hook order stays stable as config loads. Prevents the "Rendered more hooks than during the previous render" error and keeps the component idle unless there are at least 2 profiles.

  • Bug Fixes
    • Moved early return below the useEffect hooks to stabilize hook count.
    • Added optional chaining and guards in effects and deps to safely no-op when config.chatProfiles is missing or has 0–1 items.

Written for commit ecbf275. Summary will update on new commits.

The two useEffect hooks were placed after an early return, violating
React's Rules of Hooks. When config loads asynchronously (undefined →
populated), the hook count changes between renders (8 → 10), causing
React to throw "Rendered more hooks than during the previous render".

Move the early return below both useEffects and add optional chaining
guards so they no-op when config.chatProfiles is missing.
@EyalAmitay EyalAmitay force-pushed the EyalAmitay/fix-hooks-violation branch from d190937 to ecbf275 Compare April 9, 2026 12:21
@EyalAmitay EyalAmitay closed this Apr 9, 2026
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