-
-
Notifications
You must be signed in to change notification settings - Fork 199
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
[Bug]: Next.JS 15 - Hydration Failed: -className="dark" -style={{color-scheme:"dark"}} #316
Comments
Adding a mounted check to the fixed the hydration issue. Weird that this is only in Next.JS 15 but not in Next.JS 14:
|
Same issue with Next 15 For information I hadn't the issue with Next-15-RC-1 and 2. Just with the official 15 |
I'm also experiencing the same issue with Next.js 15 |
Same for me on Next.js 15.0.0 stable |
So after some debugging, I came to the conclusion that the issue can be fixed by adding a
|
This can not be the solution as it will not work on SSR and will cause the UI to flash when dark mode is selected. |
Hmm I didn't realize it wouldn't work when dark mode is selected. It doesn't flash when it is set to "system". I've reopened the issue :) |
I'm using tailwind, so I did |
This works for me: export default function RootLayout({ children }: { children: ReactNode }) {
return (
<html lang="en" className="dark" style={{ "colorScheme": "dark" }}>
<body>...</body>
</html>
);
} Theme toggling also works. For a better experience, shift the detection of color theme preference from CSS to a theme selector component or another use client component called in the root layout. Example: "use client";
import { MoonIcon, SunIcon } from "@radix-ui/react-icons";
import { Button } from "gen/ui/button";
import { useTheme } from "next-themes";
import { useEffect } from "react";
export function ThemeToggle() {
const { setTheme, theme } = useTheme();
useEffect(() => {
const isDarkTheme = globalThis.matchMedia('(prefers-color-scheme: dark)').matches
setTheme(isDarkTheme ? "dark" : "light");
}, []);
return (
<Button
data-testid="theme-toggle-button"
variant="ghost"
className="dark:hover:bg-primary/20"
onClick={() => {
setTheme(theme === "light" ? "dark" : "light");
}}
>
<SunIcon data-testid="theme-toggle-sun-icon" className="dark:hidden h-6 w-6" />
<MoonIcon data-testid="theme-toggle-moon-icon" className="hidden dark:block bg-inherit h-6 w-6" />
<span data-testid="theme-toggle-sr-text" className="sr-only">
Toggle theme
</span>
</Button>
);
} |
Me too |
For now I added
|
Another solution is to wrap your
Resolves the error/warning for me without having to add |
@Goldziher By adding Also not really sure how adding another client component changes anything. Seems like your component pretty much does what next-themes/next-themes/src/index.tsx Lines 42 to 79 in bf0c5a4
next-themes/next-themes/src/index.tsx Lines 135 to 138 in bf0c5a4
|
@AChangXD You created that white flash. NextJS pre-renders client components, but when it does that, it doesn't run The hydration error is expected. There won't be a "fix" for it. It's even mentioned in the readme. For whatever reason Next hasn't really been showing the error until now, but the behavior is the same as its always been. The correct solution is to suppress the hydration warning. |
Please read the docs: https://github.com/pacocoursey/next-themes?tab=readme-ov-file#with-app |
What happened?
After upgrading to Next.JS 15, I am now encountering a Hydration error. This error was not there in Next.JS 15 or the 15 RC version that I was using.
![image](https://private-user-images.githubusercontent.com/51896236/378664557-5cdfe705-a5d8-4f67-bf1d-ec2aeefe8eac.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3Mzk2NTUyMTEsIm5iZiI6MTczOTY1NDkxMSwicGF0aCI6Ii81MTg5NjIzNi8zNzg2NjQ1NTctNWNkZmU3MDUtYTVkOC00ZjY3LWJmMWQtZWMyYWVlZmU4ZWFjLnBuZz9YLUFtei1BbGdvcml0aG09QVdTNC1ITUFDLVNIQTI1NiZYLUFtei1DcmVkZW50aWFsPUFLSUFWQ09EWUxTQTUzUFFLNFpBJTJGMjAyNTAyMTUlMkZ1cy1lYXN0LTElMkZzMyUyRmF3czRfcmVxdWVzdCZYLUFtei1EYXRlPTIwMjUwMjE1VDIxMjgzMVomWC1BbXotRXhwaXJlcz0zMDAmWC1BbXotU2lnbmF0dXJlPTVkYTc2MWIwZTM4ZmY2YmZkZTE0MzUzMTQwZDAwMmY3OTI4YThmYjQ1OWY5ZDkxODUxNTE1NzAzZjUxZTE3Y2QmWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0In0.hVS9EpiQTtrKEf5zFcPpL_szp2uxIGVlpWCEIJLxBsw)
Adding a mounted check for
<NextThemesProvider/>
removes the hydration error but introduces another error:Version
0.3.0
What browsers are you seeing the problem on?
Safari
The text was updated successfully, but these errors were encountered: