#(solved) NextUIProvider causes Hydration mismatch

9 messages · Page 1 of 1 (latest)

steep horizon
#

Hello there,
I'm quite new to NextJS and React in general. So please forgive my lacking knowledge. Sadly I couldn't find anything on this here or on the internet. Hence this post.

I'm playing around with NextJS and NextUI but for some reason, adding the NextUIProvider as a parent of the body in layout.tsx, causes a Hydration mismatch. If I comment out the <Provider> and its closing tag, the error no longer occurs. I'm aware, that this doesn't affect the functionality of the application, but it triggers me.

My layout.tsx looks like this

'use client';

import './globals.css';
import type { Metadata } from 'next';
import { Inter } from 'next/font/google';
import { Providers } from './providers';

const inter = Inter({ subsets: ['latin'] });

export const metadata: Metadata = {
    title: 'Test',
};

export default function RootLayout({
    children,
}: {
    children: React.ReactNode;
}) {
    const bodyClasses = `${inter.className} dark text-foreground bg-background`;

    return (
        <html lang='en'>
            <Providers>
                <body className={bodyClasses}>{children}</body>
            </Providers>
        </html>
    );
}

And the providers.tsx file has the following content

'use client';

import { NextUIProvider } from '@nextui-org/react';

export function Providers({ children }: { children: React.ReactNode }) {
    return <NextUIProvider>{children}</NextUIProvider>;
}

I pretty much just copy-pasted them from the documentation.

I hope this is sufficient information. Let me know if there's anything else you need to know.
Thanks in advance.

#

Since I do not have Nitro, I have to add this in an extra message.

The layout.tsx that causes no hydration mismatch

'use client';

import './globals.css';
import type { Metadata } from 'next';
import { Inter } from 'next/font/google';
import { Providers } from './providers';

const inter = Inter({ subsets: ['latin'] });

export const metadata: Metadata = {
    title: 'Test',
};

export default function RootLayout({
    children,
}: {
    children: React.ReactNode;
}) {
    const bodyClasses = `${inter.className} dark text-foreground bg-background`;

    return (
        <html lang='en'>
            {/* <Providers> */}
            <body className={bodyClasses}>{children}</body>
            {/* </Providers> */}
        </html>
    );
}
near agate
steep horizon
# near agate Try removing "use client" from your RootLayout file

Hmm, the hydration mismatch is still there.
Just saw, that I forgot to add the error message.

Unhandled Runtime Error
Error: Hydration failed because the initial UI does not match what was rendered on the server.

Warning: Expected server HTML to contain a matching <div> in <html>.

See more info here: https://nextjs.org/docs/messages/react-hydration-error

Component Stack
div
$f57aed4a881a3485$var$OverlayContainerDOM
./node_modules/@react-aria/overlays/dist/import.mjs (911:55)
$f57aed4a881a3485$export$178405afcd8c5eb
./node_modules/@react-aria/overlays/dist/import.mjs (878:21)
$f57aed4a881a3485$export$bf688221f59024e5
$18f2051aff69b9bf$export$a54013f0d02a8f82
./node_modules/@react-aria/i18n/dist/real-module.mjs (163:19)
NextUIProvider
./node_modules/@nextui-org/system/dist/chunk-5DCY5R7Z.mjs (13:3)
Providers
./app/providers.tsx (10:11)
html
near agate
steep horizon
near agate
#

np, welcome to Nextjs

steep horizon
#

(solved) NextUIProvider causes Hydration mismatch

#

Just realized that the docs show exactly how to do it correctly and I was just too stupid to copy-paste...
Happens I guess.