You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/02-app/01-building-your-application/01-routing/02-pages-and-layouts.mdx
+3-3Lines changed: 3 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -198,12 +198,12 @@ The two layouts would be nested as such:
198
198
199
199
## Templates
200
200
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.
202
202
203
203
There may be cases where you need those specific behaviors, and templates would be a more suitable option than layouts. For example:
204
204
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.
207
207
208
208
A template can be defined by exporting a default React component from a `template.js` file. The component should accept a `children` prop.
Copy file name to clipboardExpand all lines: docs/02-app/02-api-reference/02-file-conventions/template.mdx
+5-5Lines changed: 5 additions & 5 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -3,7 +3,7 @@ title: template.js
3
3
description: API Reference for the template.js file.
4
4
---
5
5
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.
7
7
8
8
```tsx filename="app/template.tsx" switcher
9
9
exportdefaultfunction Template({ children }: { children:React.ReactNode }) {
@@ -25,7 +25,7 @@ export default function Template({ children }) {
25
25
height="444"
26
26
/>
27
27
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:
29
29
30
30
- Features that rely on `useEffect` (e.g logging page views) and `useState` (e.g a per-page feedback form).
31
31
- 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:
34
34
35
35
### `children` (required)
36
36
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:
38
38
39
39
```jsx filename="Output"
40
40
<Layout>
41
-
{/* Note that the template is given a unique key. */}
41
+
{/* Note that the template is automatically given a unique key. */}
42
42
<Template key={routeParam}>{children}</Template>
43
43
</Layout>
44
44
```
45
45
46
46
> **Good to know**:
47
47
>
48
48
> - 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.
0 commit comments