Skip to content

fix: Rules of Hooks violations hooks called after conditional early return in 5 page components #75

@g-k-s-03

Description

@g-k-s-03

What is the bug?

React requires every hook (useState, useEffect, useMemo, etc.)
to be called unconditionally on every render — in the same order, every time.

Five page components currently call hooks after an early
if (!model) return null guard. When model is null the component
exits early and those hooks are skipped. When model gets a value
those hooks suddenly run. React detects the mismatch and throws:

Rendered more hooks than during the previous render.

In development this surfaces as a crash. In production it causes
unpredictable behavior.

## Affected files (confirmed on current main)

File Early return line Hooks called after it
src/pages/OverviewPage.jsx Line 14 useState, useRef, useEffect
src/pages/RepositoriesPage.jsx Line 40 useMemo ×2, useSortedData
src/pages/ContributorsPage.jsx Line 46 useNavigate, useMemo ×2, useSortedData
src/pages/GovernancePage.jsx Line 17 useMemo
src/pages/AnalyticsPage.jsx Line 27 useMemo ×3

## Steps to Reproduce

  1. Load the app with a valid org
  2. Navigate to any affected page
  3. Clear the org so model becomes null, then reload data
  4. Browser console throws:
    Warning: React has detected a change in the order of Hooks

## Proposed Fix

Move all hook calls above the guard. Use optional chaining so hooks
work safely when model is null:

export default function ExamplePage() {
  // All hooks first, unconditionally
  const { model } = useApp()
  const allRepos = model?.allRepos ?? []
  const langs = useMemo(() => [...new Set(allRepos.map(r => r.language))], [allRepos])

  // Guard comes after all hooks
  if (!model) return null
}

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions