Skip to content
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

Hydration Mismacth from NextThemesProvider: Warning Extra attributes form the server: class, style... #168

Open
gaurangrshah opened this issue Mar 12, 2024 · 1 comment
Assignees
Labels
bug Something isn't working

Comments

@gaurangrshah
Copy link

gaurangrshah commented Mar 12, 2024

Config File

{
  "hasSrc": false,
  "packages": [
    "shadcn-ui"
  ],
  "preferredPackageManager": "pnpm",
  "t3": false,
  "alias": "@",
  "analytics": true,
  "rootPath": "",
  "componentLib": "shadcn-ui",
  "auth": null
}

Describe the bug
I am not sure if this is true for others, but I've seen this hydration warning in every project I've scaffolded with kirimase so far. I've been meaning to track down the bug and finally got some time.

Seems the NextThemesProvider is causing the hydration mismatch. The fix I found for it was to dynamically import the component and seems to work on my end. Wanted to share the solution for anyone else facing the same issue. Here's my the commit from my repo where I made the change.

Expected behavior
Should not cause hydration issues on a new scaffold.

Screenshots
image

Desktop (please complete the following information):

  • OS: [MacOS Sonoma]
  • Browser [arc, safari]
@gaurangrshah gaurangrshah added the bug Something isn't working label Mar 12, 2024
@gaurangrshah
Copy link
Author

As per this convo I dug a bit deeper.
image

From what I understand this is try this does opt the entire tree below this point which is the entire application. So this made me look for a diff solution, which I found on the next-themes github issue.

Simply adding the suppressHydrationWarning prop at least disables the message. The prop gets added to the html element in layout.tsx:

export default function RootLayout({
  children,
}: Readonly<{
  children: React.ReactNode;
}>) {
  return (
    <html lang="en" suppressHydrationWarning> {/* add the prop here */}
      <body className={GeistSans.className}>
        <Providers>
          <ThemeProvider
            attribute="class"
            defaultTheme="system"
            enableSystem
            disableTransitionOnChange
          >
            {children}
          </ThemeProvider>
        </Providers>
      </body>
    </html>
  );
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants