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/hooks/use-navigate.md
+58-19Lines changed: 58 additions & 19 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -4,6 +4,29 @@ title: useNavigate
4
4
5
5
# `useNavigate`
6
6
7
+
<details>
8
+
<summary>Type declaration</summary>
9
+
10
+
```tsx
11
+
declarefunction useNavigate():NavigateFunction;
12
+
13
+
interfaceNavigateFunction {
14
+
(to:To, options?:NavigateOptions):void;
15
+
(delta:number):void;
16
+
}
17
+
18
+
interfaceNavigateOptions {
19
+
replace?:boolean;
20
+
state?:any;
21
+
preventScrollReset?:boolean;
22
+
relative?:RelativeRoutingType;
23
+
}
24
+
25
+
typeRelativeRoutingType="route"|"path";
26
+
```
27
+
28
+
</details>
29
+
7
30
<docs-warning>It's usually better to use [`redirect`][redirect] in [`loaders`][loaders] and [`actions`][actions] than this hook</docs-warning>
8
31
9
32
The `useNavigate` hook returns a function that lets you navigate programmatically, for example in an effect:
@@ -24,31 +47,47 @@ function useLogoutTimer() {
24
47
}
25
48
```
26
49
27
-
## Type Declaration
50
+
The `navigate` function has two signatures:
28
51
29
-
```tsx
30
-
declarefunction useNavigate():NavigateFunction;
52
+
- Either pass a `To` value (same type as `<Link to>`) with an optional second `options` argument (similar to the props you can pass to [`<Link>`][link]), or
53
+
- Pass the delta you want to go in the history stack. For example, `navigate(-1)` is equivalent to hitting the back button
31
54
32
-
interfaceNavigateFunction {
33
-
(
34
-
to:To,
35
-
options?: {
36
-
replace?:boolean;
37
-
state?:any;
38
-
relative?:RelativeRoutingType;
39
-
}
40
-
):void;
41
-
(delta:number):void;
42
-
}
43
-
```
55
+
## `options.replace`
44
56
45
-
The `navigate` function has two signatures:
57
+
Specifying `replace: true` will cause the navigation will replace the current entry in the history stack instead of adding a new one.
58
+
59
+
## `options.state`
46
60
47
-
- Either pass a `To` value (same type as `<Link to>`) with an optional second `{ replace, state }` arg or
48
-
- Pass the delta you want to go in the history stack. For example, `navigate(-1)` is equivalent to hitting the back button.
61
+
You may include an optional state value in to store in [history state][history-state]
49
62
50
-
If using `replace: true`, the navigation will replace the current entry in the history stack instead of adding a new one.
63
+
## `options.preventScrollReset`
64
+
65
+
When using the [`<ScrollRestoration>`][scrollrestoration] component, you can disable resetting the scroll to the top of the page via `options.preventScrollReset`
66
+
67
+
## `options.relative`
68
+
69
+
By default, navigation is relative to the route hierarchy, so `..` will go up one `Route` level. Occasionally, you may find that you have matching URL patterns that do not make sense to be nested, and you'd prefer to use relative _path_ routing. You can opt into this behavior with `relative: true`:
70
+
71
+
```jsx
72
+
// Contact and EditContact do not share additional UI layout
0 commit comments