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
docs: Opting out of scrolling with next/link and useRouter. (#53804)
Addressing comments from #49087.
Introduced with #51869.
Related #50105.
This PR adds documentation for `next/link` and useRouter` about how to disable scroll restoration.
Copy file name to clipboardExpand all lines: docs/02-app/01-building-your-application/01-routing/03-linking-and-navigating.mdx
+19-1Lines changed: 19 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -95,7 +95,7 @@ export function Navigation({ navLinks }) {
95
95
96
96
#### Scrolling to an `id`
97
97
98
-
The default behavior of `<Link>` is to scroll to the top of a new route or to maintain the scroll position for backwards and forwards navigation.
98
+
The default behavior of the Next.js App Router is to scroll to the top of a new route or to maintain the scroll position for backwards and forwards navigation.
99
99
100
100
If you'd like to scroll to a specific `id` on navigation, you can append your URL with a `#` hash link or just pass a hash link to the `href` prop. This is possible since `<Link>` renders to an `<a>` element.
101
101
@@ -106,6 +106,24 @@ If you'd like to scroll to a specific `id` on navigation, you can append your UR
106
106
<a href="/dashboard#settings">Settings</a>
107
107
```
108
108
109
+
#### Disabling scroll restoration
110
+
111
+
The default behavior of the Next.js App Router is to scroll to the top of a new route or to maintain the scroll position for backwards and forwards navigation. If you'd like to disable this behavior, you can pass `scroll={false}` to the `<Link>` component, or `scroll: false` to `router.push()` or `router.replace()`.
112
+
113
+
```jsx
114
+
// next/link
115
+
<Link href="/dashboard" scroll={false}>
116
+
Dashboard
117
+
</Link>
118
+
```
119
+
120
+
```jsx
121
+
// useRouter
122
+
import { useRouter } from'next/navigation'
123
+
124
+
router.push('/dashboard', { scroll:false })
125
+
```
126
+
109
127
## `useRouter()` Hook
110
128
111
129
The `useRouter` hook allows you to programmatically change routes.
> **Good to know**: `<a>` tag attributes such as `className` or `target="_blank"` can be added to `<Link>` as props and will be passed to the underlying `<a>` element.
@@ -131,6 +132,34 @@ export default function Page() {
131
132
}
132
133
```
133
134
135
+
### `scroll`
136
+
137
+
**Defaults to `true`.** The default behavior of `<Link>` is to scroll to the top of a new route or to maintain the scroll position for backwards and forwards navigation. When `false`, `next/link` will _not_ scroll to the top of the page after a navigation.
138
+
139
+
```tsx filename="app/page.tsx" switcher
140
+
importLinkfrom'next/link'
141
+
142
+
exportdefaultfunction Page() {
143
+
return (
144
+
<Linkhref="/dashboard"scroll={false}>
145
+
Dashboard
146
+
</Link>
147
+
)
148
+
}
149
+
```
150
+
151
+
```jsx filename="app/page.js" switcher
152
+
importLinkfrom'next/link'
153
+
154
+
exportdefaultfunctionPage() {
155
+
return (
156
+
<Link href="/dashboard" scroll={false}>
157
+
Dashboard
158
+
</Link>
159
+
)
160
+
}
161
+
```
162
+
134
163
### `prefetch`
135
164
136
165
**Defaults to `true`.** When `true`, `next/link` will prefetch the page (denoted by the `href`) in the background. This is useful for improving the performance of client-side navigations. Any `<Link />` in the viewport (initially or through scroll) will be preloaded.
Copy file name to clipboardExpand all lines: docs/02-app/02-api-reference/04-functions/use-router.mdx
+55-11Lines changed: 55 additions & 11 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -41,8 +41,8 @@ export default function Page() {
41
41
42
42
## `useRouter()`
43
43
44
-
-`router.push(href: string)`: Perform a client-side navigation to the provided route. Adds a new entry into the [browser’s history](https://developer.mozilla.org/en-US/docs/Web/API/History_API) stack.
45
-
-`router.replace(href: string)`: Perform a client-side navigation to the provided route without adding a new entry into the [browser’s history stack](https://developer.mozilla.org/en-US/docs/Web/API/History_API).
44
+
-`router.push(href: string, { scroll: boolean })`: Perform a client-side navigation to the provided route. Adds a new entry into the [browser’s history](https://developer.mozilla.org/en-US/docs/Web/API/History_API) stack.
45
+
-`router.replace(href: string, { scroll: boolean })`: Perform a client-side navigation to the provided route without adding a new entry into the [browser’s history stack](https://developer.mozilla.org/en-US/docs/Web/API/History_API).
46
46
-`router.refresh()`: Refresh the current route. Making a new request to the server, re-fetching data requests, and re-rendering Server Components. The client will merge the updated React Server Component payload without losing unaffected client-side React (e.g. `useState`) or browser state (e.g. scroll position).
47
47
-`router.prefetch(href: string)`: [Prefetch](/docs/app/building-your-application/routing/linking-and-navigating#1-prefetching) the provided route for faster client-side transitions.
48
48
-`router.back()`: Navigate back to the previous route in the browser’s history stack.
@@ -53,18 +53,18 @@ export default function Page() {
53
53
> - The `<Link>` component automatically prefetch routes as they become visible in the viewport.
54
54
> -`refresh()` could re-produce the same result if fetch requests are cached. Other dynamic functions like `cookies` and `headers` could also change the response.
55
55
56
-
> **Migrating from the `pages` directory:**
57
-
>
58
-
> - The new `useRouter` hook should be imported from `next/navigation` and not `next/router`
59
-
> - The `pathname` string has been removed and is replaced by [`usePathname()`](/docs/app/api-reference/functions/use-pathname)
60
-
> - The `query` object has been removed and is replaced by [`useSearchParams()`](/docs/app/api-reference/functions/use-search-params)
61
-
> -`router.events`is not currently supported. [See below.](#router-events)
62
-
>
63
-
> [View the full migration guide](/docs/app/building-your-application/upgrading/app-router-migration).
56
+
### Migrating from `next/router`
57
+
58
+
- The `useRouter` hook should be imported from `next/navigation` and not `next/router` when using the App Router
59
+
- The `pathname` string has been removed and is replaced by [`usePathname()`](/docs/app/api-reference/functions/use-pathname)
60
+
- The `query` object has been removed and is replaced by [`useSearchParams()`](/docs/app/api-reference/functions/use-search-params)
61
+
-`router.events`has been replaced. [See below.](#router-events)
62
+
63
+
[View the full migration guide](/docs/app/building-your-application/upgrading/app-router-migration).
64
64
65
65
## Examples
66
66
67
-
### Router Events
67
+
### Router events
68
68
69
69
You can listen for page changes by composing other Client Component hooks like `usePathname` and `useSearchParams`.
70
70
@@ -112,6 +112,50 @@ export default function Layout({ children }) {
112
112
113
113
> **Good to know**: `<NavigationEvents>` is wrapped in a [`Suspense` boundary](/docs/app/building-your-application/routing/loading-ui-and-streaming#example) because[`useSearchParams()`](/docs/app/api-reference/functions/use-search-params) causes client-side rendering up to the closest `Suspense` boundary during [static rendering](/docs/app/building-your-application/rendering/static-and-dynamic#static-rendering-default). [Learn more](/docs/app/api-reference/functions/use-search-params#behavior).
114
114
115
+
### Disabling scroll restoration
116
+
117
+
By default, Next.js will scroll to the top of the page when navigating to a new route. You can disable this behavior by passing `scroll: false` to `router.push()` or `router.replace()`.
0 commit comments