-
Notifications
You must be signed in to change notification settings - Fork 4.1k
Fix/toggle theme hydration error #621
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 2 commits
6415396
e7f4883
5720a4e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,25 +1,35 @@ | ||
| "use client"; | ||
|
|
||
| import { Button } from "./ui/button"; | ||
| import { Sun, Moon } from "lucide-react"; | ||
| import { useTheme } from "next-themes"; | ||
| import { useEffect, useState } from "react"; | ||
|
|
||
| interface ThemeToggleProps { | ||
| className?: string; | ||
| } | ||
|
|
||
| export function ThemeToggle({ className }: ThemeToggleProps) { | ||
| const { theme, setTheme } = useTheme(); | ||
| const [isClient, setIsClient] = useState(false); | ||
|
|
||
| useEffect(() => { | ||
| setIsClient(true) | ||
| }, []) // this will see that the client side is rendered or not. | ||
|
|
||
| // if the client side is rendered then only render the button | ||
| return ( | ||
| <Button | ||
| size="icon" | ||
| variant="text" | ||
| className="h-7" | ||
| onClick={() => setTheme(theme === "dark" ? "light" : "dark")} | ||
| > | ||
| <Sun className="!size-[1.1rem]" /> | ||
| <span className="sr-only">{theme === "dark" ? "Light" : "Dark"}</span> | ||
| </Button> | ||
| <> | ||
| { | ||
| isClient && <Button | ||
| size="icon" | ||
| variant="text" | ||
| className="h-7" | ||
| onClick={() => setTheme(theme === "dark" ? "light" : "dark")} | ||
| > | ||
| {theme === "dark" ? <Sun className="!size-[1.1rem]" /> : <Moon className="!size-[1.1rem]" />} | ||
| {/* <span className="sr-only">{theme === "dark" ? "Light" : "Dark"}</span> */} | ||
|
||
| </Button> | ||
coderabbitai[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
| </> | ||
| ); | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using next-themes for dark mode generates hydration failed error
u can read it from here