Skip to content

Commit 0cf891b

Browse files
committed
fix hydration error
1 parent 065338b commit 0cf891b

1 file changed

Lines changed: 10 additions & 7 deletions

File tree

frontend/components/Navbar/LightDarkToggle.tsx

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,7 @@
33
import { useEffect, useState } from "react";
44

55
export default function LightDarkToggle() {
6-
const [theme, setTheme] = useState<string | null>(
7-
typeof window !== "undefined" ? localStorage.theme : "light",
8-
);
6+
const [theme, setTheme] = useState<string | null>(null);
97

108
function changeTheme() {
119
if (theme === "dark" || theme == undefined) {
@@ -18,9 +16,14 @@ export default function LightDarkToggle() {
1816
}
1917

2018
useEffect(() => {
21-
if (theme === undefined) {
22-
setTheme("dark");
23-
localStorage.setItem("theme", "dark");
19+
if (theme === null) {
20+
let ls = localStorage.getItem("theme");
21+
if (ls) {
22+
setTheme(ls)
23+
} else {
24+
setTheme("dark");
25+
localStorage.setItem("theme", "dark");
26+
}
2427
}
2528
if (theme) {
2629
document.body.setAttribute("data-bs-theme", theme);
@@ -30,7 +33,7 @@ export default function LightDarkToggle() {
3033
return (
3134
<div className="float-left text-center items-center mr-5 ">
3235
<button className="btn" data-testid="light-dark" onClick={changeTheme}
33-
title = { theme === "light" ? "Switch to Dark Mode" : "Switch to Light Mode" } >
36+
title={theme === "light" ? "Switch to Dark Mode" : "Switch to Light Mode"} >
3437
<i className="bi bi-lamp-fill"></i>
3538
</button>
3639
</div>

0 commit comments

Comments
 (0)