Skip to content

Commit a0d6c4f

Browse files
authored
Update docs for useNavigate() (#10749)
1 parent 868e515 commit a0d6c4f

File tree

1 file changed

+58
-19
lines changed

1 file changed

+58
-19
lines changed

docs/hooks/use-navigate.md

Lines changed: 58 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,29 @@ title: useNavigate
44

55
# `useNavigate`
66

7+
<details>
8+
<summary>Type declaration</summary>
9+
10+
```tsx
11+
declare function useNavigate(): NavigateFunction;
12+
13+
interface NavigateFunction {
14+
(to: To, options?: NavigateOptions): void;
15+
(delta: number): void;
16+
}
17+
18+
interface NavigateOptions {
19+
replace?: boolean;
20+
state?: any;
21+
preventScrollReset?: boolean;
22+
relative?: RelativeRoutingType;
23+
}
24+
25+
type RelativeRoutingType = "route" | "path";
26+
```
27+
28+
</details>
29+
730
<docs-warning>It's usually better to use [`redirect`][redirect] in [`loaders`][loaders] and [`actions`][actions] than this hook</docs-warning>
831

932
The `useNavigate` hook returns a function that lets you navigate programmatically, for example in an effect:
@@ -24,31 +47,47 @@ function useLogoutTimer() {
2447
}
2548
```
2649

27-
## Type Declaration
50+
The `navigate` function has two signatures:
2851

29-
```tsx
30-
declare function 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
3154

32-
interface NavigateFunction {
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`
4456

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`
4660

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]
4962

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
73+
<Route path="/" element={<Layout />}>
74+
<Route path="contacts/:id" element={<Contact />} />
75+
<Route
76+
path="contacts/:id/edit"
77+
element={<EditContact />}
78+
/>
79+
</Route>;
80+
81+
function EditContact() {
82+
// Since Contact is not a parent of EditContact we need to go up one level
83+
// in the path, instead of one level in the Route hierarchy
84+
navigate("..", { relative: "path" });
85+
}
86+
```
5187

88+
[link]: ../components/link
5289
[redirect]: ../fetch/redirect
5390
[loaders]: ../route/loader
5491
[actions]: ../route/action
92+
[history-state]: https://developer.mozilla.org/en-US/docs/Web/API/History/state
93+
[scrollrestoration]: ../components/scroll-restoration

0 commit comments

Comments
 (0)