Next.js Discord

Discord Forum

Hydration error on creating a new layout.tsx

Answered
Anay-208 | Ping on replies posted this in #help-forum
Open in Discord
Avatar
Once I create a new layout.tsx, I keep getting tons of hydration Error
Answered by Eric Burel
Use route groups to isolate the one with a different layout, or use a client component to detect the current routes
View full answer

16 Replies

Avatar
Here is layout.tsx:
import type { Metadata } from "next";
import "@/app/globals.css";
import Footer from "@/components/footer";
import { Toaster } from "react-hot-toast";
import fonts from "@/fonts/fonts";
import { cn } from "@/lib/utils";
export const runtime = "edge"


export default function RootLayout({
  children,
}: Readonly<{
  children: React.ReactNode;
}>) {
  return (
    <html lang="en">
      <body className={cn(
        "min-h-screen bg-background font-sans antialiased",
        fonts.map((font) => font.variable).join(" ")
      )}>
        {/* only navbar is removed */}
        <Toaster />
        {children}
        <Footer />
        </body>
    </html>
  );
}
I get errors like
Warning: Expected server HTML to contain a matching <main> in <body>.
Error: Hydration failed because the initial UI does not match what was rendered on the server.
and for my home route, Styles stop rendering until I restart app, but after some time, same thing happens
Image
What I'm trying to do is remove navbar for a specific route(/cart), like on first step of form, I want the navbar and on second step of form, I don't want it
Avatar
Is your Toaster component working with SSR?
It's an old project so maybe it doesn't
First you'll want to remove each component and see what happens
you may want to compare the HTML result in the netwrok tab (first request) and what your have in the elements tab (rendered client-side)
Here are some strategies to handle hydration errors for components taht do not support server rendering
https://medium.com/@eric.burel/how-to-get-rid-of-window-is-not-defined-and-hydration-mismatch-errors-in-next-js-567cc51b4a17
Avatar
Got it, So I want to remove Navbar from 1 route. How can I do that?
Avatar
@Anay-208 | Ping on replies Got it, So I want to remove `Navbar` from 1 route. How can I do that?
Avatar
Use route groups to isolate the one with a different layout, or use a client component to detect the current routes
Answer
Avatar
Alright, thanks!