# The template.js Convention

> Like a layout, but intentionally destroys and rebuilds state on navigation.

Subject: Next.js | Lesson 8 of 65 | 5 min read
Interactive lesson: https://codewithanimation.in/nextjs/template-file
Previous: [The Root Layout](https://codewithanimation.in/nextjs/root-layout.md) | Next: [The loading.js Convention](https://codewithanimation.in/nextjs/loading-file.md)

## The anti-layout

The `template.js` (or `.tsx`) file convention is the counterpart to `layout.js`. Like a layout, a template wraps a child segment or page and must accept a `{children}` prop.

However, there is one massive difference: while layouts persist their state across route changes, templates **create a completely new instance** for each child on navigation.

## Why destroy state?

If preserving state is great for performance, why would you ever want a template? Sometimes, you specifically *need* a component to remount from scratch.

Common use cases include: triggering page-transition animations (which require a fresh mount), resetting form inputs when moving between steps in a wizard, or forcing a `useEffect` to run on every single page view.

## Nesting order

You create a template exactly like a layout: export a React component from `template.js` that renders `{children}`.

If you place both a `layout.js` and a `template.js` in the exact same folder, Next.js enforces a strict nesting order: The Layout will always wrap the Template. `<Layout><Template><Page /></Template></Layout>`.

## Common mistake: The performance trap

The biggest mistake developers make is using `template.js` for their main application shell (like sidebars and navbars) instead of `layout.js`.

If you do this, your entire sidebar will unmount, destroy its state, and rebuild itself from scratch every time the user clicks a link. This destroys the app-like feel and degrades performance.

## Tip: Default to Layouts

As a strict rule of thumb: use `layout.js` for 99% of your shared UI.

Only reach for `template.js` if you are explicitly trying to solve a problem caused by layout state persisting (like stuck scroll positions in a specific UI pattern, or complex entry animations).

## Try it: Layout vs Template

Type text into both the Layout and Template inputs, then click the navigation buttons. Watch what happens to the state.

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/nextjs/template-file

## Check yourself

### Question 1

What is the primary difference between a layout.js and a template.js?

- A. Layouts can fetch data, but templates cannot
- B. Templates preserve state on navigation, layouts do not
- C. Layouts preserve state on navigation, while templates create a new instance (remount)
- D. Templates do not require a 'children' prop

Correct answer: C

Layouts are built for performance and state persistence. Templates are built for intentional state destruction and remounting.

- Why not A: Both can fetch data.
- Why not B: It's exactly the opposite.
- Why not D: Both strictly require a children prop.

### Question 2

If a folder contains both a layout.js and a template.js, how does Next.js structure the component tree?

- A. <Template><Layout><Page /></Layout></Template>
- B. <Layout><Template><Page /></Template></Layout>
- C. Next.js throws an error; you cannot have both in one folder
- D. Next.js ignores the template and only renders the layout

Correct answer: B

Next.js guarantees that the layout wraps the template. This allows the layout to persist across navigations while the template inside it remounts.

- Why not A: The layout must be the outermost wrapper so it can persist.
- Why not C: You can absolutely have both in the same folder.
- Why not D: Both are rendered.

## Remember this

- Templates wrap routes exactly like layouts.
- Unlike layouts, templates remount and destroy their state on every navigation.
- Use templates for page transitions or when you explicitly want state to reset.
- If both exist in a folder, the Layout always wraps the Template.
