Layouts compose by folder
Each segment can declare its own layout.tsx. They nest automatically: a child layout renders inside its parent. The render tree is exactly what the folder tree looks like.
app/layout.tsx → wraps everything
app/dashboard/layout.tsx → wraps /dashboard/*
app/dashboard/settings/page.tsx → rendered inside both layouts
What persists, what re-renders
Navigating from /dashboard/settings to /dashboard/billing only swaps the page; both layouts stay mounted. Navigating from /dashboard/settings to /marketing/pricing unmounts the dashboard layout and mounts whatever wraps marketing.
layout.tsx vs template.tsx
Use layout.tsx by default. Use template.tsx when you need fresh state or enter/exit animations on every navigation — it re-mounts each time. The name in the folder is the only difference; the props are the same.
Draw the route tree before choosing where state lives. State shared by every dashboard page belongs in the dashboard layout; state shared by the whole application belongs at the root. The common ancestor of two URLs tells you which layout survives their transition. Using template.tsx to force a remount can hide state-placement bugs, but it also discards form input and cached client state. Reach for a template only when a fresh instance on every navigation is the intended experience, such as a replayed entrance transition.
Log mount and unmount events for root, dashboard, and page nodes while navigating within and outside the dashboard. Repeat with a template. If the only evidence is that the screen looks right, you have not verified the lifetime contract.