Skip to content

Commit

Permalink
fix: ssr hydration
Browse files Browse the repository at this point in the history
  • Loading branch information
jxom committed Feb 5, 2024
1 parent a19eb31 commit 7171da0
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 7 deletions.
5 changes: 5 additions & 0 deletions .changeset/bright-paws-attend.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"vocs": patch
---

Fixed SSR hydration issue in footer navigation.
11 changes: 5 additions & 6 deletions src/app/components/Footer.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import { Pencil2Icon } from '@radix-ui/react-icons'
import { assignInlineVars } from '@vanilla-extract/dynamic'
import clsx from 'clsx'
import { useEffect, useMemo, useState } from 'react'
import { useEffect, useMemo } from 'react'
import { useLocation, useNavigate } from 'react-router-dom'
import { Footer as ConsumerFooter } from 'virtual:consumer-components'

import type { SidebarItem } from '../../config.js'
import { useEditLink } from '../hooks/useEditLink.js'
import { useLayout } from '../hooks/useLayout.js'
import { useMounted } from '../hooks/useMounted.js'
import { usePageData } from '../hooks/usePageData.js'
import { useSidebar } from '../hooks/useSidebar.js'
import * as styles from './Footer.css.js'
Expand All @@ -20,13 +21,9 @@ import { ArrowRight } from './icons/ArrowRight.js'

export function Footer() {
const { layout } = useLayout()
const mounted = useMounted()
const pageData = usePageData()

const [mounted, setMounted] = useState(false)
useEffect(() => {
setMounted(true)
}, [])

const lastUpdatedAtDate = useMemo(
() => (pageData.lastUpdatedAt ? new Date(pageData.lastUpdatedAt) : undefined),
[pageData.lastUpdatedAt],
Expand Down Expand Up @@ -76,6 +73,7 @@ function EditLink() {
}

function Navigation() {
const mounted = useMounted()
const sidebar = useSidebar()

const { pathname } = useLocation()
Expand Down Expand Up @@ -128,6 +126,7 @@ function Navigation() {
}
}, [])

if (!mounted) return null
return (
<div className={styles.navigation}>
{prevPage ? (
Expand Down
2 changes: 1 addition & 1 deletion src/app/components/Outline.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ export function Outline({
if (!active.current) return

if (window.scrollY === 0) {
setActiveId(items[0].id)
setActiveId(items[0]?.id)
return
}

Expand Down
9 changes: 9 additions & 0 deletions src/app/hooks/useMounted.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { useEffect, useState } from 'react'

export function useMounted() {
const [mounted, setMounted] = useState(false)
useEffect(() => {
setMounted(true)
}, [])
return mounted
}

0 comments on commit 7171da0

Please sign in to comment.