`template.js` vs `layout.js`: When State Should Reset

A layout persists across navigations and keeps its state. A template remounts on every navigation within its segment. Learn when that difference matters.

6 min read

A Next.js template.js file and a layout.js file both wrap a page with shared UI, but they behave differently across navigation. A layout renders once and stays mounted, keeping its state, while a template remounts every time the segment it belongs to changes.

App.tsxApp.tsx
// app/blog/layout.tsx
export default function BlogLayout({
  children,
}: {
  children: React.ReactNode;
}) {
  return <div className="blog-shell">{children}</div>;
}

Navigating between two posts under this layout keeps the layout mounted the whole time. Any state inside it, such as a scroll position tracked in a Client Component, survives the navigation.

What changes with a template

Swap that same file for a template.tsx, and Next.js gives it a unique key tied to its segment. Navigating to a different route inside that segment remounts the template and everything inside it.

App.tsxApp.tsx
// app/blog/template.tsx
export default function BlogTemplate({
  children,
}: {
  children: React.ReactNode;
}) {
  return <div className="blog-shell">{children}</div>;
}

The markup looks identical to the layout version, but the behavior does not. A Client Component holding local state inside this template resets that state on every navigation between blog posts, because React treats each navigation as a fresh mount.

Where a template sits in the hierarchy

A template renders between a layout and the page, wrapping loading.tsx, error.tsx, and not-found.tsx in the same segment. Having both files at once is valid: the layout still persists, while the template inside it still remounts on navigation.

This ordering also changes how loading fallbacks behave. A Suspense fallback inside a layout only shows on the very first load of that segment, but the same fallback inside a template shows again on every navigation, since the template itself remounts each time. For the difference between not-found handling at the segment level versus the whole app, see not-found.js vs global-not-found.js.

When the reset actually matters

NeedReach for
Shared UI that should keep its state across navigationlayout
Resetting a Client Component's local state on every navigationtemplate
Re-running a useEffect on every navigation within a segmenttemplate
Showing a Suspense fallback on every navigation, not just first loadtemplate

A common real case is an input field that should clear itself every time a visitor moves to a new item in a list. A layout would leave stale text sitting in the field; a template clears it automatically because the input remounts.

Common mistake

Reaching for a template by default is a common mistake, since the remount behavior has a real cost: every Client Component inside it rebuilds its DOM and loses state on each navigation, even state a visitor would expect to persist, like an open dropdown. Start with a layout, and only switch to a template once a specific reset requirement shows up. For the full rules on how layouts nest and share data with pages, see layouts in Next.js.

Rune AI

Rune AI

Key Insights

  • A layout persists across navigations within its segment and keeps component state.
  • A template remounts on every navigation within its own segment, resetting state.
  • Both accept the same children prop and render between a parent layout and the page.
  • A template also resynchronizes effects like useEffect on every navigation.
  • Use a template only when the reset behavior is genuinely needed, not by default.
RunePowered by Rune AI

Frequently Asked Questions

Is a template.js file required in every route?

No. Most routes only need a layout. Add a template only when a segment specifically needs its state or effects to reset on every navigation within it.

Does a template reset state on every navigation anywhere in the app?

No. It only remounts when the segment that owns the template, including its dynamic params, changes. Navigating within a deeper child segment does not remount a parent template.

Can a route have both a layout and a template at the same level?

Yes. Next.js renders the template between the layout and the page in that case, so the layout still persists while the template still remounts on navigation.

Conclusion

A layout renders once and stays mounted across navigations within it, while a template remounts every time its segment changes. Reach for a template only when that reset behavior, not the shared UI itself, is what a route actually needs.