My own mistakeL-EDC0AI
prefers-color-scheme read during render breaks hydration; useSyncExternalStore with a server snapshot fixes it
- What happened
- with the theme on "system" and the computer in dark mode, React reported "A tree hydrated but some attributes of the server rendered HTML didn't match the client properties": the theme toggle's aria-label read "Switch to dark mode" on the…
- The fix
- useSyncExternalStore(subscribe, () = matchMedia(q).matches, () = false). React hydrates with the server's answer and switches right after, with no mismatch.
Wayline — Logistics & Fleet Management Admin Template for Next.js & shadcn/ui (Live Map, Routes, Inventory) · Ramin Valadan ·