search

Home  >  Q&A  >  body text

Can the root layout component be hidden in some nested routes inside the Next.js application folder?

Is there any way to prevent rootlayout from being wrapped by dashboardlayout? Next.js v13 Documentation:

My file structure:

I could use route groups; however, doing so would disable wrapping in my contact, pricing routes. Is there any way to prevent this from happening? I want the home navigation bar to be on the contact information and pricing pages, but I don't want the home navigation bar to be on the dashboard.

I tried using routing groups; however, it disabled wrapping in my pricing and contact routes.

navbar.tsx

"use client";

import { useEffect, useState } from "react";
import { Disclosure } from "@headlessui/react";


function joinClassName(...classes: string[]) {
  return classes.filter(Boolean).join(" ");
}


export default function Navbar() {
  const [navbar, setNavbar] = useState(false);

  const changeBackground = () => {
    if (window.scrollY >= 64) {
      setNavbar(true);
    } else {
      setNavbar(false);
    }
  };

  useEffect(() => {
    changeBackground();
    window.addEventListener("scroll", changeBackground);
  });

  return (
    <Disclosure as="nav">
      {({ open, close }) => (
        <>
          <div
            className={joinClassName(
              navbar || open ? "dark:bg-black bg-white" : "bg-transparent",
              " fixed top-0 left-0 right-0 z-50 "
            )}
          ></div>
        </>
      )}
    </Disclosure>
  );
}

P粉418351692P粉418351692272 days ago422

reply all(2)I'll reply

  • P粉982054449

    P粉9820544492024-03-27 09:56:37

    Since your Navbar is a client component, you can avoid using the routing group, but can do so by using usePathname/dashboard>, like this:

    "use client";
    
    // Other imports ...
    
    import { usePathname } from "next/navigation";
    
    export default function Navbar() {
      const pathname = usePathname();
      // useEffect and other codes ...
    
      if (pathname == "/dashboard") {
        return <>;
      }
      return 
    Actual content
    ; }

    reply
    0
  • P粉807239416

    P粉8072394162024-03-27 09:09:48

    After some digging I managed to get it working with route groups.

    File structure

    /app/layout.tsx

    export default function RootLayout({
      children,
    }: {
      children: React.ReactNode;
    }) {
      return (
        
          {children}
        
      );
    }

    /app/(dash)/dashboard/layout.tsx

    export default function DashboardLayout({
      children,
    }: {
      children: React.ReactNode;
    }) {
      return 
    {children}
    ; }

    /app/(Login)/layout.tsx

    export default function LandingLayout({
      children,
    }: {
      children: React.ReactNode;
    }) {
      return (
        
    {children}
    ); }

    Youssouf's solution works great. However, the dashboard route still has the rootlayout CSS styles and other components, which requires me to manually add lines of code to components that I don't want to appear in /dashboard.

    reply
    0
  • Cancelreply