0

Layouts & nested layouts

Routingused in ~95% of apps

Every real app has a chrome that stays put — a top bar, a sidebar, a footer — while the middle swaps as you click around. Layouts are how Next.js gives you that for free. Misunderstand one thing about them (that they persist and do NOT re-render on navigation) and you will write fetches that never refresh and waste hours debugging a sidebar that keeps resetting.

Core idea. A layout.tsx is a special file that wraps every page in its folder and below, receiving the matched page as a children prop. Layouts nest by folder depth, and — this is the part tutorials get wrong — a layout PERSISTS across navigation within its segment: it keeps its state and does NOT re-render when you move between the pages it wraps.
Playground, this isn't a screenshot
route config
createBrowserRouter([
{ path: "/app", element: <AppLayout/>,
children: [
]
}
]);

Click a child route to "navigate". The pathless parent (<AppLayout/>) wraps every child.

example.com/app
layout mounted 1×
AppLayout
sidebar (layout)

This input lives in the layout. Switch tabs — your text stays.

<Outlet /> contentchild mounted 1× (fresh)
Dashboardindex: true

The index route — shown by default when the URL is exactly the parent path.

The pathless <AppLayout/> mounts once and stays put — only the <Outlet/> swaps as you navigate. Your sidebar text and the layout mounted 1× badge prove it. With no child segment, the index route fills the Outlet.

↑ play a little first, then dig in