Skip to content

Commit 6464441

Browse files
authored
Update useNavigate reference page (#1388)
1 parent e53d4f0 commit 6464441

File tree

1 file changed

+143
-15
lines changed

1 file changed

+143
-15
lines changed

src/routes/solid-router/reference/primitives/use-navigate.mdx

Lines changed: 143 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,31 +9,159 @@ tags:
99
- programmatic
1010
- history
1111
- state
12-
version: '1.0'
12+
version: "1.0"
1313
description: >-
1414
Navigate programmatically with useNavigate - redirect users, handle auth
1515
flows, and control navigation with replace and scroll options.
1616
---
1717

18-
Retrieves the method which accepts a path to navigate to and an optional object with the following options:
18+
The `useNavigate` function provides a function for programmatically navigating to a new route.
1919

20-
- resolve (_boolean_, default `true`): resolve the path against the current route
21-
- replace (_boolean_, default `false`): replace the history entry
22-
- scroll (_boolean_, default `true`): scroll to top after navigation
23-
- state (_any_, default `undefined`): pass custom state to `location.state`
20+
## Import
21+
22+
```ts
23+
import { useNavigate } from "@solidjs/router";
24+
```
25+
26+
## Type
27+
28+
```ts
29+
interface NavigateOptions<S = unknown> {
30+
resolve: boolean;
31+
replace: boolean;
32+
scroll: boolean;
33+
state: S;
34+
}
35+
36+
function useNavigate(): (
37+
to: string,
38+
options?: Partial<NavigateOptions>
39+
) => void;
40+
function useNavigate(delta: number): void;
41+
```
42+
43+
## Parameters
44+
45+
`useNavigate` takes no arguments.
46+
47+
## Return value
48+
49+
- **Type:** `(to: string | number, options?: NavigateOptions) => void | (delta: number) => void`
50+
51+
Returns a function that accepts two arguments:
52+
53+
### `to`
54+
55+
- **Type:** `string | number`
56+
- **Required:** Yes
57+
58+
The target destination.
59+
60+
- `string`:
61+
A path to navigate to.
62+
- `number`:
63+
A history delta (e.g., `-1` for back, `1` for forward).
64+
If provided, the `options` argument is ignored.
65+
66+
### `options`
67+
68+
- **Type:** `NavigateOptions`
69+
- **Required:** No
70+
71+
Configuration object for the navigation.
72+
73+
#### `resolve`
74+
75+
- **Type:** `boolean`
76+
- **Default:** `true`
77+
78+
Resolves the path relative to the current route.
79+
If `false`, the path is resolved against the root (`/`).
80+
81+
If `to` is a query-only string (e.g., `?sort=asc`), this defaults to `false` to preserve the current pathname.
82+
83+
#### `replace`
84+
85+
- **Type**: `boolean`
86+
- **Default**: `false`
87+
88+
Replaces the current history entry instead of adding a new one.
89+
Used for redirects or state updates to prevent the user from navigating back to the previous state.
90+
91+
#### `scroll`
92+
93+
- **Type**: `boolean`
94+
- **Default**: `true`
95+
96+
Scrolls the window to the top after navigation.
97+
98+
- `true`:
99+
Scrolls to the top or to the element matching the hash.
100+
- `false`:
101+
Maintains the current scroll position (unless a hash matches).
102+
103+
#### `state`
104+
105+
- **Type**: `any`
106+
- **Default**: `undefined`
107+
108+
Arbitrary state stored in `history.state`.
109+
This value is accessible via `useLocation().state`.
110+
111+
State is serialized using the [structured clone algorithm](https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Structured_clone_algorithm), which supports most built-in types but not functions or DOM nodes.
112+
113+
## Examples
114+
115+
### Basic usage
116+
117+
```tsx
118+
import { useNavigate } from "@solidjs/router";
119+
120+
const navigate = useNavigate();
121+
122+
navigate("/users/123");
123+
```
124+
125+
### With `replace`
126+
127+
```tsx
128+
import { useNavigate } from "@solidjs/router";
129+
130+
const navigate = useNavigate();
131+
132+
// Redirect (replace history)
133+
function login() {
134+
navigate("/dashboard", { replace: true });
135+
}
136+
```
137+
138+
### With `delta`
139+
140+
```tsx
141+
import { useNavigate } from "@solidjs/router";
24142

25-
```js
26143
const navigate = useNavigate();
27144

28-
if (unauthorized) {
29-
navigate("/login", { replace: true });
145+
// Go back one page
146+
function goBack() {
147+
navigate(-1);
30148
}
31149
```
32150

33-
If you are inside of a query, action or cache (deprecated) function you will instead want to use [redirect](/solid-router/reference/response-helpers/redirect) or [reload](/solid-router/reference/response-helpers/reload).
151+
### With `state`
152+
153+
```tsx
154+
import { useNavigate } from "@solidjs/router";
155+
156+
const navigate = useNavigate();
157+
158+
// Pass custom state
159+
navigate("/checkout", {
160+
state: { from: "cart", total: 100 },
161+
});
162+
```
163+
164+
## Related
34165

35-
:::note
36-
The state is serialized using the [structured clone
37-
algorithm](https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Structured_clone_algorithm)
38-
which does not support all object types.
39-
:::
166+
- [useLocation](/solid-router/reference/primitives/use-location)
167+
- [redirect](/solid-router/reference/response-helpers/redirect)

0 commit comments

Comments
 (0)