Skip to content

Commit ad16071

Browse files
fix hydration in theme toggle
1 parent 8843072 commit ad16071

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed

components/NavbarComponents/ThemeToggle.tsx

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,15 @@
22

33
import { Moon, Sun } from "lucide-react";
44
import { useTheme } from "next-themes";
5-
import { useEffect } from "react";
5+
import { useEffect, useState } from "react";
66

77
export default function ThemeToggle() {
88
const { theme, setTheme } = useTheme();
9+
const [mounted, setMounted] = useState(false);
10+
11+
useEffect(() => {
12+
setMounted(true);
13+
}, []);
914

1015
useEffect(() => {
1116
const handleKeyPress = (e: KeyboardEvent) => {
@@ -19,6 +24,8 @@ export default function ThemeToggle() {
1924
return () => window.removeEventListener("keydown", handleKeyPress);
2025
}, [theme, setTheme]);
2126

27+
if (!mounted) return null;
28+
2229
return (
2330
<button
2431
onClick={() => setTheme(theme === "dark" ? "light" : "dark")}

0 commit comments

Comments
 (0)