Skip to content

Commit b7eb7d3

Browse files
Docs: Clarify template.js behavior (#64781)
1 parent f7fef23 commit b7eb7d3

2 files changed

Lines changed: 8 additions & 8 deletions

File tree

docs/02-app/01-building-your-application/01-routing/02-pages-and-layouts.mdx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -198,12 +198,12 @@ The two layouts would be nested as such:
198198
199199
## Templates
200200

201-
Templates are similar to layouts in that they wrap 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. This means that 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.
201+
Templates are similar to layouts in that they wrap a 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. This means that when a user navigates between routes that share a template, a new instance of the child is mounted, DOM elements are recreated, state is **not** preserved in Client Components, and effects are re-synchronized.
202202

203203
There may be cases where you need those specific behaviors, and templates would be a more suitable option than layouts. For example:
204204

205-
- Features that rely on `useEffect` (e.g logging page views) and `useState` (e.g a per-page feedback form).
206-
- To change the default framework behavior. For example, Suspense Boundaries inside layouts only show the fallback the first time the Layout is loaded and not when switching pages. For templates, the fallback is shown on each navigation.
205+
- To resynchronize `useEffect` on navigation.
206+
- To reset the state of a child Client Components on navigation.
207207

208208
A template can be defined by exporting a default React component from a `template.js` file. The component should accept a `children` prop.
209209

docs/02-app/02-api-reference/02-file-conventions/template.mdx

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ title: template.js
33
description: API Reference for the template.js file.
44
---
55

6-
A **template** file is similar to a [layout](/docs/app/building-your-application/routing/pages-and-layouts#layouts) 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.
6+
A **template** file is similar to a [layout](/docs/app/building-your-application/routing/pages-and-layouts#layouts) in that it wraps a layout or page. Unlike layouts that persist across routes and maintain state, templates are given a unique key, meaning children Client Components reset their state on navigation.
77

88
```tsx filename="app/template.tsx" switcher
99
export default function Template({ children }: { children: React.ReactNode }) {
@@ -25,7 +25,7 @@ export default function Template({ children }) {
2525
height="444"
2626
/>
2727

28-
While less common, you might choose a template over a layout if you want:
28+
While less common, you might choose to use a template over a layout if you want:
2929

3030
- Features that rely on `useEffect` (e.g logging page views) and `useState` (e.g a per-page feedback form).
3131
- To change the default framework behavior. For example, Suspense Boundaries inside layouts only show the fallback the first time the Layout is loaded and not when switching pages. For templates, the fallback is shown on each navigation.
@@ -34,19 +34,19 @@ While less common, you might choose a template over a layout if you want:
3434

3535
### `children` (required)
3636

37-
Template components should accept and use a `children` prop. `template` is rendered between a [layout](/docs/app/api-reference/file-conventions/layout) and its children. For example:
37+
Template accepts a `children` prop. For example:
3838

3939
```jsx filename="Output"
4040
<Layout>
41-
{/* Note that the template is given a unique key. */}
41+
{/* Note that the template is automatically given a unique key. */}
4242
<Template key={routeParam}>{children}</Template>
4343
</Layout>
4444
```
4545

4646
> **Good to know**:
4747
>
4848
> - By default, `template` is a [Server Component](/docs/app/building-your-application/rendering/server-components), but can also be used as a [Client Component](/docs/app/building-your-application/rendering/client-components) through the `"use client"` directive.
49-
> - 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.
49+
> - 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 in Client Components, and effects are re-synchronized.
5050
5151
## Version History
5252

0 commit comments

Comments
 (0)