Layout persistence and templates
Layouts can be reused during client-side navigation. A template is a related file convention that creates a keyed wrapper which remounts when its own route segment changes. Choose persistence for useful continuity and remounting only when state should reset.
Scope: Next.js 16.x candidate, App Router only, Cache Components disabled, TypeScript unless otherwise labeled. Examples are static source walkthroughs; exact framework patch and build are not verified.
app/catalog/layout.tsx (alternative to previous lesson)
import type { ReactNode } from 'react';import ReadingNote from './ReadingNote';export default function Layout({ children }: { children: ReactNode }) { return <section><ReadingNote />{children}</section>;}A client input inside this shared layout is expected to retain its value on soft navigation between catalog descendants that reuse the layout. Reloading the document is not the same test.
State belongs to a mounted component
A mount creates a component instance; an unmount removes it. A Client Component uses the use client directive to allow interactive React state. Place this example in the same folder as the layout. A template is nested inside a layout: if the note is moved into a template at app/catalog/[slug]/template.tsx, navigation between two slug values remounts that template. Changes only to query parameters do not trigger template remounting; deeper segment changes do not necessarily reset an ancestor template.
app/catalog/ReadingNote.tsx
'use client';import { useState } from 'react';export default function ReadingNote() { const [note, setNote] = useState(''); return <label>Reading note <input value={note} onChange={e => setNote(e.target.value)} /></label>;}Typing a synthetic note changes the input state. Compare the shared layout placement with a template that imports this component from ../ReadingNote. In the template variant the wrapper is a default export accepting children, just as above.
Common mistake
Do not assume a template resets on every URL change or that layout state persists after a hard refresh, navigation across root layouts, or removal of the shared subtree.
Try it yourself
Type a note on /catalog/moss, navigate to /catalog/harbor, change the query string, then reload. Write separate expected observations for the two placements. No real personal notes are needed.
After this lesson, you should be able to: Predict which wrapper is reused; Identify when a template is appropriate.
Further reading: Root document, nested layouts and reuse, Segment-keyed remount behavior.