Next.js Discord

Discord Forum

Trying to wrap my head around App Router

Answered
Aleutian Tern posted this in #help-forum
Open in Discord
Aleutian TernOP
I'm trying to wrap my head around app router. So if I have a layout that's like this sidebar:

import Link from "next/link"
import { Avatar, AvatarFallback, AvatarImage } from "@/components/ui/avatar"

export default function DashboardLayout({
    children, // will be a page or nested layout
  }: {
    children: React.ReactNode
  }) {
    return (
      <section>
            <aside className="w-64 h-screen bg-gray-100 p-4 flex flex-col justify-between">
            <div>
                <div className="flex items-center space-x-2 mb-4">
                <LampIcon className="text-orange-500 w-6 h-6" />
                <span className="font-bold text-sm">Lantern</span>
                </div>
                <nav className="flex flex-col gap-4">
                <Link className="flex items-center space-x-2 text-gray-900 hover:bg-gray-200 p-1 rounded" href="#">
                    <HomeIcon className="w-4 h-4" />
                    <span>Home</span>
                </Link>
                <Link className="flex items-center space-x-2 text-gray-900 hover:bg-gray-200 p-1 rounded" href="#">
                    <MessagesSquareIcon className="w-4 h-4" />
                    <span>Messages</span>
                </Link>
                <Link className="flex items-center space-x-2 text-gray-900 hover:bg-gray-200 p-1 rounded" href="#">
                    <LinkIcon className="w-4 h-4" />
                    <span>Partnerships</span>
...

            </div>
            </aside>
        <nav></nav>
   
        {children}
      </section>
    )
  }


Then do I have each individual route in it's own directory? Like /dashboard as a route with it's own page.tsx file and then I'd pull that into the root page.tsx?
Answered by aardani
if you have a sidebar and you want that to show in ALL pages under /dashboard, (including /dashboard itself) then you'd put the side bar in /dashboard/layout.tsx
View full answer

12 Replies

if you try to access /dashboard then the following will be rendered:
/dashboard/page.tsx
/dashboard/layout.tsx
/layout.tsx
.
/dashboard/page.tsx
will be put under the {children} of
/dashboard/layout.tsx (if any)
and that will be put under the {children} of
/layout.tsx
if you have a sidebar and you want that to show in ALL pages under /dashboard, (including /dashboard itself) then you'd put the side bar in /dashboard/layout.tsx
Answer
@aardani . `/dashboard/page.tsx` will be put under the {children} of `/dashboard/layout.tsx` (if any) and that will be put under the {children} of `/layout.tsx`
Aleutian TernOP
Ok interesting. Makes sense. And if I wanted to situate the child content beside the sidebar I'd just need to handle that with html right?
@aardani yeah, I usually do it with flex or grid
Aleutian TernOP
Amazing thank you this answers my question 💪
@Aleutian Tern Amazing thank you this answers my question 💪
No problem! please mark a message as the answer to your post!
@aardani No problem! please mark a message as the answer to your post!
Aleutian TernOP
How do I do that? :lolsob:
In mobile, hold the message, Apps > Mark Solution
Aleutian TernOP
Oh dopeeee
Awesome, thanks