Templates and segment boundaries
What you need to know
Use a template when part of an interface should start fresh as its route segment changes. A shared layout often retains its mounted client state during navigation; a template receives a segment-based identity and can remount its subtree. This is useful for a note form that must not accidentally carry text from one book to another.
By the end, you can: compare remount and persistence; predict state reset.
app/books/[slug]/template.tsx
import type { ReactNode } from "react";import Note from "../Note";export default function Template({ children }: { children: ReactNode }) { return <section><Note />{children}</section>;}Expected behavior (not framework-tested): For the proposed tree app/books/[slug]/page.tsx, switching the active book segment is expected to reset the note inside this template, which is placed at [slug], not its static books ancestor. Verify against the chosen framework patch.
How it works
A layout wraps the template; the template wraps the page or deeper segments. State in the template subtree is lost when that template remounts. The identity belongs to the relevant segment, not every URL change. Deeper changes do not necessarily remount ancestor templates, and query-only changes do not by themselves change template identity. An explicit reset button may be simpler if navigation is unrelated to the state lifecycle.
app/books/Note.tsx
"use client";import { useState } from "react";export default function Note() { const [text, setText] = useState(""); return <label>Reading note <input value={text} onChange={e => setText(e.target.value)} /></label>;}Expected behavior (not framework-tested): Type a note on /books/moss, navigate to /books/tide, and compare the input with a version mounted in books/layout.tsx. Expected: the template version resets when its key changes, the shared layout version remains.
Common mistake
A template is not an automatic reset for search parameters or all descendant transitions. Test the exact segment boundary; do not put durable unsaved work inside a resetting subtree without warning.
Practice and verification
Compare direct load, soft navigation between book slugs and a query-only change. Record where a note persists. Use the same input component in both variants so the boundary is the only change.
Scope: App Router, proposed Next.js 16.x, Cache Components disabled for these labs. Snippets assume a reviewed TypeScript app and are static examples; framework behavior has not been executed. Exact supported patch and dependency set must be verified before running.