Skip to content

Commit 31ec98d

Browse files
committed
fix hydration issue in nav
1 parent b00ec38 commit 31ec98d

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

src/components/Homepage/LanguageMorpher.tsx

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,12 @@ import {
1111
MOBILE_LANGUAGE_BUTTON_NAME,
1212
} from "@/lib/constants"
1313

14+
import { useIsClient } from "@/hooks/useIsClient"
1415
import { useMediaQuery } from "@/hooks/useMediaQuery"
1516

1617
const LanguageMorpher = () => {
18+
const isClient = useIsClient()
19+
1720
const handleMobileClick = () => {
1821
;(document.getElementById(HAMBURGER_BUTTON_ID) as HTMLButtonElement).click()
1922
setTimeout(
@@ -36,10 +39,14 @@ const LanguageMorpher = () => {
3639

3740
const [isLarge] = useMediaQuery([`(min-width: ${screens.md})`])
3841

42+
// Use fallback value during SSR to prevent hydration mismatch
43+
// Default to false (mobile) during SSR, then use actual value on client
44+
const isLargeScreen = isClient ? isLarge : false
45+
3946
return (
4047
<Button
4148
className="mx-auto w-fit text-md text-primary no-underline"
42-
onClick={isLarge ? handleDesktopClick : handleMobileClick}
49+
onClick={isLargeScreen ? handleDesktopClick : handleMobileClick}
4350
variant="ghost"
4451
>
4552
<Morpher

src/components/Nav/Client/index.tsx

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import { useNavigation } from "../useNavigation"
1616
import { useThemeToggle } from "../useThemeToggle"
1717

1818
import { useBreakpointValue } from "@/hooks/useBreakpointValue"
19+
import { useIsClient } from "@/hooks/useIsClient"
1920
import { useTranslation } from "@/hooks/useTranslation"
2021

2122
const LazyButton = dynamic(
@@ -86,9 +87,14 @@ const ClientSideNav = () => {
8687
const { toggleColorMode, ThemeIcon, themeIconAriaLabel } = useThemeToggle()
8788

8889
const languagePickerRef = useRef<HTMLButtonElement>(null)
90+
const isClient = useIsClient()
8991

9092
// avoid rendering/adding desktop Menu version to DOM on mobile
91-
const desktopScreen = useBreakpointValue({ base: false, md: true })
93+
const desktopScreenValue = useBreakpointValue({ base: false, md: true })
94+
95+
// Use fallback value during SSR to prevent hydration mismatch
96+
// Default to false (mobile) during SSR, then use actual value on client
97+
const desktopScreen = isClient ? desktopScreenValue : false
9298

9399
return (
94100
<>

0 commit comments

Comments
 (0)