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
|[`prefetch`](#prefetch)|`prefetch={false}`| Boolean or null | - |
80
+
|[`onNavigate`](#onnavigate)|`onNavigate={(e) => {}}`| Function | - |
79
81
80
82
</AppOnly>
81
83
@@ -450,6 +452,58 @@ export default function Home() {
450
452
451
453
</PagesOnly>
452
454
455
+
### `onNavigate`
456
+
457
+
An event handler called during client-side navigation. The handler receives an event object that includes a `preventDefault()` method, allowing you to cancel the navigation if needed.
458
+
459
+
```tsx filename="app/page.tsx" switcher
460
+
importLinkfrom'next/link'
461
+
462
+
exportdefaultfunction Page() {
463
+
return (
464
+
<Link
465
+
href="/dashboard"
466
+
onNavigate={(e) => {
467
+
// Only executes during SPA navigation
468
+
console.log('Navigating...')
469
+
470
+
// Optionally prevent navigation
471
+
// e.preventDefault()
472
+
}}
473
+
>
474
+
Dashboard
475
+
</Link>
476
+
)
477
+
}
478
+
```
479
+
480
+
```jsx filename="app/page.js" switcher
481
+
importLinkfrom'next/link'
482
+
483
+
exportdefaultfunctionPage() {
484
+
return (
485
+
<Link
486
+
href="/dashboard"
487
+
onNavigate={(e) => {
488
+
// Only executes during SPA navigation
489
+
console.log('Navigating...')
490
+
491
+
// Optionally prevent navigation
492
+
// e.preventDefault()
493
+
}}
494
+
>
495
+
Dashboard
496
+
</Link>
497
+
)
498
+
}
499
+
```
500
+
501
+
> **Good to know**: While `onClick` and `onNavigate` may seem similar, they serve different purposes. `onClick` executes for all click events, while `onNavigate` only runs during client-side navigation. Some key differences:
502
+
>
503
+
> - When using modifier keys (`Ctrl`/`Cmd` + Click), `onClick` executes but `onNavigate` doesn't since Next.js prevents default navigation for new tabs.
504
+
> - External URLs won't trigger `onNavigate` since it's only for client-side and same-origin navigations.
505
+
> - Links with the `download` attribute will work with `onClick` but not `onNavigate` since the browser will treat the linked URL as a download.
506
+
453
507
## Examples
454
508
455
509
The following examples demonstrate how to use the `<Link>` component in different scenarios.
@@ -1178,10 +1232,287 @@ export default function Home() {
1178
1232
1179
1233
</PagesOnly>
1180
1234
1235
+
### Blocking navigation
1236
+
1237
+
You can use the `onNavigate` prop to block navigation when certain conditions are met, such as when a form has unsaved changes. When you need to block navigation across multiple components in your app (like preventing navigation from any link while a form is being edited), React Context provides a clean way to share this blocking state. First, create a context to track the navigation blocking state:
|`v13.0.0`| No longer requires a child `<a>` tag. A [codemod](/docs/app/building-your-application/upgrading/codemods#remove-a-tags-from-link-components) is provided to automatically update your codebase. |
1186
1517
|`v10.0.0`|`href` props pointing to a dynamic route are automatically resolved and no longer require an `as` prop. |
0 commit comments