Skip to content

fix(header): sync theme icon state after mount#83

Open
QuentinHsu wants to merge 1 commit intoshareAI-lab:mainfrom
QuentinHsu:fix/web-theme-mount-sync
Open

fix(header): sync theme icon state after mount#83
QuentinHsu wants to merge 1 commit intoshareAI-lab:mainfrom
QuentinHsu:fix/web-theme-mount-sync

Conversation

@QuentinHsu
Copy link

Problem

  • The Header component reads theme state during initial render, which can cause the first client render to disagree with the server-rendered icon.
  • The theme toggle renders light/dark icons before mount, creating a hydration mismatch risk.

Solution

  • Change the dark state to start from a stable default and sync the actual theme from document.documentElement in useEffect after mount.
  • Add a mounted flag and render a placeholder before the component is mounted.
  • Keep the existing theme toggle behavior unchanged while adjusting only the initial sync and render timing.

Testing

  • Verify the header theme icon renders consistently on first page load.
  • Verify both desktop and mobile toggle buttons update correctly after switching themes.
  • Verify the icon stays aligned with the current document theme after a page refresh.

- initialize dark mode state after mount from document to avoid mismatched first render state.
- render a placeholder before mount to reduce theme icon hydration mismatch risk.
@vercel
Copy link

vercel bot commented Mar 15, 2026

@QuentinHsu is attempting to deploy a commit to the crazyboym's projects Team on Vercel.

A member of the Team first needs to authorize it.

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