fix: use useSyncExternalStore for hydration guard to satisfy React lint rule
This commit is contained in:
@@ -4,7 +4,7 @@ import { AnimatePresence, motion } from "motion/react"
|
|||||||
import logo from "@/app/icon.png"
|
import logo from "@/app/icon.png"
|
||||||
import Image from "next/image"
|
import Image from "next/image"
|
||||||
import Link from "next/link"
|
import Link from "next/link"
|
||||||
import { useEffect, useRef, useState } from "react"
|
import { useEffect, useSyncExternalStore, useRef, useState } from "react"
|
||||||
import {
|
import {
|
||||||
Bookmark,
|
Bookmark,
|
||||||
CircleXIcon,
|
CircleXIcon,
|
||||||
@@ -51,8 +51,9 @@ export default function Navbar() {
|
|||||||
// Prevent hydration mismatch: useSession resolves differently on
|
// Prevent hydration mismatch: useSession resolves differently on
|
||||||
// server (isPending=true) vs client. We delay rendering the
|
// server (isPending=true) vs client. We delay rendering the
|
||||||
// auth-dependent UI until after the first client paint.
|
// auth-dependent UI until after the first client paint.
|
||||||
const [mounted, setMounted] = useState(false)
|
// useSyncExternalStore avoids the React lint warning about setState in effects.
|
||||||
useEffect(() => setMounted(true), [])
|
const subscribe = () => () => {}
|
||||||
|
const mounted = useSyncExternalStore(subscribe, () => true, () => false)
|
||||||
const showAuth = mounted && !isSessionLoading
|
const showAuth = mounted && !isSessionLoading
|
||||||
|
|
||||||
// Sync search query with URL ?q= param
|
// Sync search query with URL ?q= param
|
||||||
|
|||||||
Reference in New Issue
Block a user