Question presented to candidate: "In the Next.js App Router, what is a layout, how do layouts nest, and what actually happens to a layout when the user clicks between two pages that share it?"
What a strong answer should cover:
- A layout is a
layout.tsxfile whose component receiveschildrenand wraps every page in that folder and below it. - The root layout in
app/is required and must render<html>and<body>; nested layouts stack by folder (root > dashboard > settings > page). - On client navigation, shared layouts are kept: they do not re-render or remount, so their state (and Client Components inside them) survives.
- Consequences: layouts cannot read
searchParamsand should not depend on the pathname; use a Client Component withusePathnameor the page props instead. They cannot pass props tochildren. template.tsxis the alternative that remounts on every navigation; route groups and multiple root layouts change which layout applies.
Clarifying questions expected:
- "Should this UI keep its state between pages (a sidebar, a player) or reset (an animation, a form)?" β that is the layout versus template choice.
- "Do different sections need entirely different shells?" β route groups or multiple root layouts.
Code / implementation expected: Yes β a root layout plus one nested layout with two pages.