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

[DashboardLayout] color-scheme hydration warnings #4491

Closed
ashishjaswal2002 opened this issue Nov 29, 2024 · 32 comments
Closed

[DashboardLayout] color-scheme hydration warnings #4491

ashishjaswal2002 opened this issue Nov 29, 2024 · 32 comments
Labels
bug 🐛 Something doesn't work

Comments

@ashishjaswal2002
Copy link

ashishjaswal2002 commented Nov 29, 2024

Screenshot 2024-11-29 at 3 42 03 PM

Search keywords:

Search keywords:

@github-actions github-actions bot added the status: waiting for maintainer These issues haven't been looked at yet by a maintainer label Nov 29, 2024
@ashishjaswal2002
Copy link
Author

ashishjaswal2002 commented Nov 29, 2024

this is my Nextjs layout.jsx code

import { AppRouterCacheProvider } from '@mui/material-nextjs/v15-appRouter';
Provider
import localFont from "next/font/local";
import "./globals.css";
import Provider from '../util/Providers';
import DashboardLayoutBasic from '@/components/dashboardLayout/page';


const geistSans = localFont({
  src: "./fonts/GeistVF.woff",
  variable: "--font-geist-sans",
  weight: "100 900",
});
const geistMono = localFont({
  src: "./fonts/GeistMonoVF.woff",
  variable: "--font-geist-mono",
  weight: "100 900",
});

export const metadata = {
  title: "one7sportsAdminPanel_Frontend",
  description: "Admin panel frontend.",
};

export default function RootLayout({ children }) {
  return (
    <html lang="en">
      <body
        className={`${geistSans.variable} ${geistMono.variable} antialiased`}
      >

  
    <Provider>

   <AppRouterCacheProvider>

    <DashboardLayoutBasic>
      {children}
    </DashboardLayoutBasic>
   </AppRouterCacheProvider>

    </Provider>


      </body>
    </html>
  );
}

@ashishjaswal2002
Copy link
Author

ashishjaswal2002 commented Nov 29, 2024

'use client'
import * as React from 'react';
import PropTypes from 'prop-types';
import Box from '@mui/material/Box';
import Typography from '@mui/material/Typography';
import { createTheme } from '@mui/material/styles';
import DashboardIcon from '@mui/icons-material/Dashboard';
import ShoppingCartIcon from '@mui/icons-material/ShoppingCart';
import BarChartIcon from '@mui/icons-material/BarChart';
import DescriptionIcon from '@mui/icons-material/Description';
import LayersIcon from '@mui/icons-material/Layers';
import { AppProvider } from '@toolpad/core/AppProvider';
import { DashboardLayout } from '@toolpad/core/DashboardLayout';
import { useDemoRouter } from '@toolpad/core/internal';

const NAVIGATION = [
  {
    kind: 'header',
    title: 'Main items',
  },
  {
    segment: 'dashboard',
    title: 'Dashboard',
    icon: <DashboardIcon />,
  },
  {
    segment: 'orders',
    title: 'Orders',
    icon: <ShoppingCartIcon />,
  },
  {
    kind: 'divider',
  },
  {
    kind: 'header',
    title: 'Analytics',
  },
  {
    segment: 'reports',
    title: 'Reports',
    icon: <BarChartIcon />,
    children: [
      {
        segment: 'sales',
        title: 'Sales',
        icon: <DescriptionIcon />,
      },
      {
        segment: 'traffic',
        title: 'Traffic',
        icon: <DescriptionIcon />,
      },
    ],
  },
  {
    segment: 'integrations',
    title: 'Integrations',
    icon: <LayersIcon />,
  },
];

const demoTheme = createTheme({
  cssVariables: {
    colorSchemeSelector: 'data-toolpad-color-scheme',
  },
  colorSchemes: { light: true, dark: true },
  breakpoints: {
    values: {
      xs: 0,
      sm: 600,
      md: 600,
      lg: 1200,
      xl: 1536,
    },
  },
});

function DemoPageContent({ pathname }) {
  return (
    <Box
      sx={{
        py: 4,
        display: 'flex',
        flexDirection: 'column',
        alignItems: 'center',
        textAlign: 'center',
      }}
    >
      <Typography>Dashboard content for {pathname}</Typography>
    </Box>
  );
}

DemoPageContent.propTypes = {
  pathname: PropTypes.string.isRequired,
};

function DashboardLayoutBasic(props) {
  const { window } = props;

  const router = useDemoRouter('/dashboard');

  // Remove this const when copying and pasting into your project.
  const demoWindow = window !== undefined ? window() : undefined;

  return (
    // preview-start
    <AppProvider
      navigation={NAVIGATION}
      router={router}
      theme={demoTheme}
      window={demoWindow}
    >
      <DashboardLayout>
        <DemoPageContent pathname={router.pathname} />
      </DashboardLayout>
    </AppProvider>
    // preview-end
  );
}

DashboardLayoutBasic.propTypes = {
  /**
   * Injected by the documentation to work in an iframe.
   * Remove this when copying and pasting into your project.
   */
  window: PropTypes.func,
};

export default DashboardLayoutBasic;

this is my another component named DashboardLayoutBasic which i importing in layout.jsx

@aarongarciah
Copy link
Member

Hey @ashishjaswal2002, please provide a playground or repository so we can take a look.

@aarongarciah aarongarciah self-assigned this Nov 29, 2024
@aarongarciah aarongarciah added status: waiting for author Issue with insufficient information and removed status: waiting for maintainer These issues haven't been looked at yet by a maintainer labels Nov 29, 2024
@aarongarciah aarongarciah transferred this issue from mui/material-ui Nov 29, 2024
@ashishjaswal2002
Copy link
Author

@github-actions github-actions bot added status: waiting for maintainer These issues haven't been looked at yet by a maintainer and removed status: waiting for author Issue with insufficient information labels Nov 29, 2024
@ashishjaswal2002
Copy link
Author

not working i am using this as an example https://mui.com/toolpad/core/introduction/examples/

@bharatkashyap
Copy link
Member

not working i am using this as an example https://mui.com/toolpad/core/introduction/examples/

Is it possible to create a minimal reproduction in CodeSandbox to make it possible to debug this? By minimal reproduction, I mean adding the minimum amount of code it takes to reproduce this error

@ashishjaswal2002
Copy link
Author

I m using next 15 i think on sand box it has different version

@bharatkashyap
Copy link
Member

I m using next 15 i think on sand box it has different version

You can create your own sandbox to demonstrate the error

@ashishjaswal2002
Copy link
Author

@bharatkashyap did you find the error

@ashishjaswal2002
Copy link
Author

ashishjaswal2002 commented Dec 2, 2024

@bharatkashyap i figured out the issue i wrap the childrens in themeProvider but the issue is Where should i write my navigation or AppProvider component which provides navigation?

import React from "react";
import { AppRouterCacheProvider } from "@mui/material-nextjs/v15-appRouter";
import localFont from "next/font/local";
import "./globals.css";
import Provider from "../util/Providers";
import { AppProvider } from "@toolpad/core/nextjs";
import DashboardIcon from "@mui/icons-material/Dashboard";
import ShoppingCartIcon from "@mui/icons-material/ShoppingCart";
import demoTheme from "@/theme/theme";
import {ThemeProvider} from '@mui/material/styles'
const NAVIGATION = [
  {
    kind: "header",
    title: "Main items",
  },
  {
    title: "Dashboard",
    icon: <DashboardIcon />,
  },
  {
    segment: "orders",
    title: "Orders",
    icon: <ShoppingCartIcon />,
  },
];

const geistSans = localFont({
  src: "./fonts/GeistVF.woff",
  variable: "--font-geist-sans",
  weight: "100 900",
});
const geistMono = localFont({
  src: "./fonts/GeistMonoVF.woff",
  variable: "--font-geist-mono",
  weight: "100 900",
});

export const metadata = {
  title: "Something",
  description: "Spometjing",
};

export default  function RootLayout({ children }) {
  return (
    <html lang="en" >
      <body
        className={`${geistSans.variable} ${geistMono.variable} antialiased`} >
        <Provider>
          <AppRouterCacheProvider options={{ enableCssLayer: true }} >
            
           <ThemeProvider  theme={demoTheme}>

            {children}
            
            </ThemeProvider>

          </AppRouterCacheProvider>
        </Provider>
      </body>
    </html>
  );
}

@ashishjaswal2002
Copy link
Author

ashishjaswal2002 commented Dec 2, 2024

if i add App provider inside themeProvider it gives me hydration error
NextJS15

import React from "react";
import { AppRouterCacheProvider } from "@mui/material-nextjs/v15-appRouter";
import localFont from "next/font/local";
import "./globals.css";
import Provider from "../util/Providers";
import { AppProvider } from "@toolpad/core/nextjs";
import DashboardIcon from "@mui/icons-material/Dashboard";
import ShoppingCartIcon from "@mui/icons-material/ShoppingCart";
import demoTheme from "@/theme/theme";
import {ThemeProvider} from '@mui/material/styles'
const NAVIGATION = [
  {
    kind: "header",
    title: "Main items",
  },
  {
    title: "Dashboard",
    icon: <DashboardIcon />,
  },
  {
    segment: "orders",
    title: "Orders",
    icon: <ShoppingCartIcon />,
  },
];

const geistSans = localFont({
  src: "./fonts/GeistVF.woff",
  variable: "--font-geist-sans",
  weight: "100 900",
});
const geistMono = localFont({
  src: "./fonts/GeistMonoVF.woff",
  variable: "--font-geist-mono",
  weight: "100 900",
});

export const metadata = {
  title: "osfaasafs",
  description: "asfasfas",
};

export default  function RootLayout({ children }) {
  return (
    <html lang="en" >
      <body
        className={`${geistSans.variable} ${geistMono.variable} antialiased`} >
        <Provider>
          <AppRouterCacheProvider options={{ enableCssLayer: true }} >
            
           <ThemeProvider  theme={demoTheme}>

            <AppProvider>

            {children}

            </AppProvider>
            
            </ThemeProvider>

          </AppRouterCacheProvider>
        </Provider>
      </body>
    </html>
  );
}
```

@ashishjaswal2002
Copy link
Author

@aarongarciah please give me guidance.

@bharatkashyap
Copy link
Member

bharatkashyap commented Dec 2, 2024

if i add App provider inside themeProvider it gives me hydration error NextJS15

Not sure if I understand your need correctly but you should be able to pass the theme object directly to the theme prop of the AppProvider. Refer the following docs section https://mui.com/toolpad/core/react-app-provider/#theming

@ashishjaswal2002
Copy link
Author

Screenshot 2024-12-02 at 3 25 01 PM Still getting same issue

@ashishjaswal2002
Copy link
Author

ashishjaswal2002 commented Dec 2, 2024

import React from "react";
import { AppRouterCacheProvider } from "@mui/material-nextjs/v15-appRouter";
import localFont from "next/font/local";
import "./globals.css";
import Provider from "../util/Providers";
import DashboardIcon from "@mui/icons-material/Dashboard";
import ShoppingCartIcon from "@mui/icons-material/ShoppingCart";
import demoTheme from "@/theme/theme";
import { ThemeProvider } from "@mui/material/styles";
import { AppProvider } from "@toolpad/core/nextjs";
import logo from "@assets/Images/Logo.png";

const navigation = [
  {
    segment: "dashboard",
    title: "Dashboard",
    icon: <DashboardIcon />,
  },
  {
    segment: "ground-manager",
    title: "Ground Manager",
    icon: <ShoppingCartIcon />,
    children: [
      {
        segment: "view-grounds",
        title: "View Grounds",
        icon: <ShoppingCartIcon />,
      },
    ],
  },
  {
    segment: "banner-manager",
    title: "Banner Manager",
    icon: <ShoppingCartIcon />,
    children: [
      {
        segment: "add-banners",
        title: "Add New Banner",
        icon: <ShoppingCartIcon />,
      },
      {
        segment: "view-banners",
        title: "View All Banners",
        icon: <ShoppingCartIcon />,
      },
    ],
  },
  {
    segment: "tournament-manager",
    title: "Tournament Manager",
    icon: <ShoppingCartIcon />,
    children: [
      {
        segment: "add-tournament",
        title: "Add Tournament",
        icon: <ShoppingCartIcon />,
      },
      {
        segment: "view-tournament",
        title: "View Tournament",
        icon: <ShoppingCartIcon />,
      },
      {
        segment: "user-tournament/register",
        title: "Registered User",
        icon: <ShoppingCartIcon />,
      },
    ],
  },
  {
    segment: "user-manager",
    title: "User Manager",
    icon: <ShoppingCartIcon />,
    children: [
      {
        segment: "ground-owners",
        title: "View Ground Owner ",
        icon: <ShoppingCartIcon />,
      },
      {
        segment: "user-registered",
        title: "View Users",
        icon: <ShoppingCartIcon />,
      },
    ],
  },
  {
    segment: "support-manager",
    title: "Support Manager",
    icon: <ShoppingCartIcon />,
    children: [
      {
        segment: "user-support",
        title: "User Support",
        icon: <ShoppingCartIcon />,
      },
    ],
  },
  {
    segment: "notification-manager",
    title: "Notification Manager",
    icon: <ShoppingCartIcon />,
    children: [
      {
        segment: "push-notification",
        title: "App Notification",
        icon: <ShoppingCartIcon />,
      },
    ],
  },
];
const geistSans = localFont({
  src: "./fonts/GeistVF.woff",
  variable: "--font-geist-sans",
  weight: "100 900",
});
const geistMono = localFont({
  src: "./fonts/GeistMonoVF.woff",
  variable: "--font-geist-mono",
  weight: "100 900",
});

export const metadata = {
  title: "asdasdas",
  description: "adsasdasd",
};

export default function RootLayout({ children }) {
  return (
    <html lang="en">
      <body
        className={`${geistSans.variable} ${geistMono.variable} antialiased`}
      >
        <Provider>
          <AppRouterCacheProvider>
            <AppProvider
              theme={demoTheme}
              navigation={navigation}
              branding={{
                logo: <img src={logo} alt="logo" />,
                title: "",
              }}
            >
              {children}
            </AppProvider>
          </AppRouterCacheProvider>
        </Provider>
      </body>
    </html>
  );
}

this is my code Here Provider is React Query --->

"use client";

import { QueryClientProvider,QueryClient } from "@tanstack/react-query";
import { ReactQueryDevtools } from "@tanstack/react-query-devtools";
import {  useState } from "react";



export default  function  Provider({children}){

    const[queryClient] = useState(()=> new QueryClient({

      defaultOptions:{
        queries:{
          staleTime:6*1000,
          refetchInterval:6*1000,
          cacheTime: 1000 * 60 * 60 * 24, //24 hourss...
        },
      }
    }));



    return(

        <QueryClientProvider client={queryClient}>
          
          <ReactQueryDevtools initialIsOpen={false} />

          {children}

        </QueryClientProvider>
    )

}

@ashishjaswal2002
Copy link
Author

@bharatkashyap

@bharatkashyap
Copy link
Member

@bharatkashyap

Try adding data-toolpad-color-scheme="light" to the html tag in the Root Layout

@apedroferreira apedroferreira added bug 🐛 Something doesn't work and removed status: waiting for maintainer These issues haven't been looked at yet by a maintainer labels Dec 2, 2024
@apedroferreira
Copy link
Member

@bharatkashyap

Try adding data-toolpad-color-scheme="light" to the html tag in the Root Layout

This should fix the problem for now, but before we close this issue let's try to find out why it is required to add data-toolpad-color-scheme="light" to the html tag and make it so that it's not required.

@ashishjaswal2002
Copy link
Author

Screenshot 2024-12-02 at 5 46 50 PM still same issue

@ashishjaswal2002
Copy link
Author

MY updated Code in NextJS RootLayout.


import React from "react";
import { AppRouterCacheProvider } from "@mui/material-nextjs/v15-appRouter";
import localFont from "next/font/local";
import "./globals.css";
import Provider from "../util/Providers";
import DashboardIcon from "@mui/icons-material/Dashboard";
import ShoppingCartIcon from "@mui/icons-material/ShoppingCart";
import demoTheme from "@/theme/theme";
import { ThemeProvider } from "@mui/material/styles";
import { AppProvider } from "@toolpad/core/nextjs";
import logo from "@assets/Images/Logo.png";

const navigation = [
  {
    segment: "dashboard",
    title: "Dashboard",
    icon: <DashboardIcon />,
  },
  {
    segment: "ground-manager",
    title: "Ground Manager",
    icon: <ShoppingCartIcon />,
    children: [
      {
        segment: "view-grounds",
        title: "View Grounds",
        icon: <ShoppingCartIcon />,
      },
    ],
  },
  {
    segment: "banner-manager",
    title: "Banner Manager",
    icon: <ShoppingCartIcon />,
    children: [
      {
        segment: "add-banners",
        title: "Add New Banner",
        icon: <ShoppingCartIcon />,
      },
      {
        segment: "view-banners",
        title: "View All Banners",
        icon: <ShoppingCartIcon />,
      },
    ],
  },
  {
    segment: "tournament-manager",
    title: "Tournament Manager",
    icon: <ShoppingCartIcon />,
    children: [
      {
        segment: "add-tournament",
        title: "Add Tournament",
        icon: <ShoppingCartIcon />,
      },
      {
        segment: "view-tournament",
        title: "View Tournament",
        icon: <ShoppingCartIcon />,
      },
      {
        segment: "user-tournament/register",
        title: "Registered User",
        icon: <ShoppingCartIcon />,
      },
    ],
  },
  {
    segment: "user-manager",
    title: "User Manager",
    icon: <ShoppingCartIcon />,
    children: [
      {
        segment: "ground-owners",
        title: "View Ground Owner ",
        icon: <ShoppingCartIcon />,
      },
      {
        segment: "user-registered",
        title: "View Users",
        icon: <ShoppingCartIcon />,
      },
    ],
  },
  {
    segment: "support-manager",
    title: "Support Manager",
    icon: <ShoppingCartIcon />,
    children: [
      {
        segment: "user-support",
        title: "User Support",
        icon: <ShoppingCartIcon />,
      },
    ],
  },
  {
    segment: "notification-manager",
    title: "Notification Manager",
    icon: <ShoppingCartIcon />,
    children: [
      {
        segment: "push-notification",
        title: "App Notification",
        icon: <ShoppingCartIcon />,
      },
    ],
  },
];
const geistSans = localFont({
  src: "./fonts/GeistVF.woff",
  variable: "--font-geist-sans",
  weight: "100 900",
});
const geistMono = localFont({
  src: "./fonts/GeistMonoVF.woff",
  variable: "--font-geist-mono",
  weight: "100 900",
});

export const metadata = {
  title: "dasadsasd",
  description: "Some Description.",
};

export default function RootLayout({ children }) {
  return (
    <html lang="en"  data-toolpad-color-scheme="light">
      <body
        className={`${geistSans.variable} ${geistMono.variable} antialiased`}
      >
        <Provider>
          <AppRouterCacheProvider>
            <AppProvider
              theme={demoTheme}
              navigation={navigation}
              branding={{
                logo: <img src={logo} alt="logo" />,
                title: "",
              }}
            >
              {children}
            </AppProvider>
          </AppRouterCacheProvider>
        </Provider>
      </body>
    </html>
  );
}

@bharatkashyap
Copy link
Member

MY updated Code in NextJS RootLayout

Try changing the value of the attribute to dark

@ashishjaswal2002
Copy link
Author

ashishjaswal2002 commented Dec 2, 2024

@bharatkashyap
Same ERROR

Console Error

Hydration failed because the server rendered HTML didn't match the client. As a result this tree will be regenerated on the client. This can happen if a SSR-ed Client Component used

- A server/client branch `if (typeof window !== 'undefined')`.
- Variable input such as `Date.now()` or `Math.random()` which changes each time it's called.
- Date formatting in a user's locale which doesn't match the server.
- External changing data without sending a snapshot of it along with the HTML.
- Invalid HTML tag nesting.

It can also happen if the client has a browser extension installed which messes with the HTML before React loaded.

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


 ...
  <Router actionQueue={{state:{...}, ...}} assetPrefix="">
    <HistoryUpdater>
      <RuntimeStyles>
        <HotReload assetPrefix="">
          <ReactDevOverlay state={{nextId:1, ...}} dispatcher={{...}}>
            <DevRootNotFoundBoundary>
              <NotFoundBoundary notFound={<NotAllowedRootNotFoundError>}>
                <NotFoundErrorBoundary pathname="/dashboard" notFound={<NotAllowedRootNotFoundError>} ...>
                  <RedirectBoundary>
                    <RedirectErrorBoundary router={{...}}>
                      <Head>
                        <link>
                          <script>
                            <script>
                              <script>
                                <script>
                                  <script>
                                    <script>
                                      <script>
                                        <script>
                                          <script>
                                            <script>
                                              <RootLayout>
                                                <html
                                                  lang="en"
                                                    >
                                                      ...
                                                        ...
+                                                    data-toolpad-color-scheme="dark"
-                                                    data-toolpad-color-scheme="light"

@ashishjaswal2002
Copy link
Author

ashishjaswal2002 commented Dec 2, 2024

@bharatkashyap @aarongarciah @apedroferreira
If i follow this then how can i integrate Dashboard Layout. It is not mentioned here that how can we use Dashboard Layout.
https://mui.com/material-ui/integrations/nextjs/

Hydration Error is not occured if i following this but it is mentioned different in dashboard layout.
https://mui.com/toolpad/core/react-dashboard-layout/

It is very confusing.

@bharatkashyap
Copy link
Member

@bharatkashyap @aarongarciah @apedroferreira If i follow this then how can i integrate Dashboard Layout. It is not mentioned here that how can we use Dashboard Layout. https://mui.com/material-ui/integrations/nextjs/

Hydration Error is not occured if i following this but it is mentioned different in dashboard layout. https://mui.com/toolpad/core/react-dashboard-layout/

It is very confusing.

Try to follow these docs: https://mui.com/toolpad/core/integrations/nextjs-approuter/

@bharatkashyap
Copy link
Member

bharatkashyap commented Dec 2, 2024

I cannot reproduce the error anymore - the hydration warning goes away even in the Sandbox you've provided on adding the data-toolpad-color-scheme attribute

I think it might not be very productive to have this discussion on a GitHub issue. @ashishjaswal2002 Please open a question on Stack Overflow with a reproduction link and we can hopefully find an answer there 👍🏻

@bharatkashyap bharatkashyap changed the title How to import Dashboard Layout toolpad core in nextjs i am getting some errors. [DashboardLayout] color-scheme hydration warnings Dec 2, 2024
@ashishjaswal2002
Copy link
Author

ashishjaswal2002 commented Dec 2, 2024

Let me following the article again if i didn't find the issue then i m closing by myself.

@ashishjaswal2002
Copy link
Author

It is working fine but there is one problem
According to Material UI -> Create a Dashboard Layout.
https://mui.com/toolpad/core/integrations/nextjs-approuter/
if i don't use (use client ) it is giving me error. but if i used it below in my next js app.It is showing no error.

'use client'
import React from 'react';
import { DashboardLayout, PageContainer } from "@toolpad/core";


export default  function DashboardLayoutUi({children}){
  return (

<>
<DashboardLayout>


{children}


</DashboardLayout>

</>

  )


}

@ashishjaswal2002
Copy link
Author

@bharatkashyap

@ashishjaswal2002
Copy link
Author

Material Ui should add
'use client' on their Next Js integration documents. I am closing this issue as of now.

@ashishjaswal2002
Copy link
Author

Closed

@github-project-automation github-project-automation bot moved this from Backlog to Completed in MUI Toolpad public roadmap Dec 3, 2024
Copy link

github-actions bot commented Dec 3, 2024

This issue has been closed. If you have a similar problem but not exactly the same, please open a new issue.
Now, if you have additional information related to this issue or things that could help future readers, feel free to leave a comment.

Note

@ashishjaswal2002 How did we do? Your experience with our support team matters to us. If you have a moment, please share your thoughts in this short Support Satisfaction survey.

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

No branches or pull requests

4 participants