A template file is similar to a layout in that it wraps each child layout or page. Unlike layouts that persist across routes and maintain state, templates create a new instance for each of their children on navigation.
export default function Template({ children }: { children: React.ReactNode }) {
return <div>{children}</div>
}

While less common, you might choose a template over a layout if you want:
useEffect (e.g logging page views) and useState (e.g a per-page feedback form).Template components should accept and use a children prop. template is rendered between a layout and its children. For example:
<Layout>
{/* Note that the template is given a unique key. */}
<Template key={routeParam}>{children}</Template>
</Layout>Good to know:
- By default,
templateis a Server Component, but can also be used as a Client Component through the"use client"directive.- When a user navigates between routes that share a
template, a new instance of the component is mounted, DOM elements are recreated, state is not preserved, and effects are re-synchronized.
| Version | Changes |
|---|---|
v13.0.0 |
template introduced. |
© 2024 Vercel, Inc.
Licensed under the MIT License.
https://nextjs.org/docs/app/api-reference/file-conventions/template