|
2 | 2 | title: "Navigation" |
3 | 3 | --- |
4 | 4 |
|
5 | | -Once your routes have been created within an application, [anchor tags](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/a) can be used to help users navigate between different views or pages. |
| 5 | +When using Solid Router, you can use the standard standard HTML [`<a>` elements](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/a), which triggers [soft navigation](/solid-router/reference/components/a#soft-navigation). |
| 6 | +In addition to using this, Solid Router offers other options for navigating between routes: |
6 | 7 |
|
7 | | -```jsx {4-5} |
8 | | -const App = (props) => ( |
9 | | - <> |
10 | | - <nav> |
11 | | - <a href="/users">Users</a> |
12 | | - <a href="/">Home</a> |
13 | | - </nav> |
14 | | - <h1>My Site with lots of pages</h1> |
15 | | - {props.children} |
16 | | - </> |
17 | | -); |
18 | | -``` |
| 8 | +- The [`<A>` component](/solid-router/reference/components/a). |
| 9 | +- The [`useNavigate` primitive](/solid-router/reference/primitives/use-navigate). |
| 10 | +- The [`redirect` function](/solid-router/reference/response-helpers/redirect). |
19 | 11 |
|
20 | 12 | ## `<A>` component |
21 | 13 |
|
22 | | -While you can use the native HTML anchor tag, Solid Router does provide a [`<A>`](/solid-router/reference/components/a) component. |
23 | | -While similar to an anchor tag, the `<A>` component supports automatically applying the base URL path and relative paths. |
24 | | - |
25 | | -```jsx {4-5} |
26 | | -const App = (props) => ( |
27 | | - <> |
28 | | - <nav> |
29 | | - <A href="/">Home</A> |
30 | | - <A href="/users">Users</A> |
31 | | - </nav> |
32 | | - <h1>My Site with lots of pages</h1> |
33 | | - {props.children} |
34 | | - </> |
35 | | -); |
| 14 | +The `<A>` component extends the native `<a>` element by automatically applying the base URL path and, additionally, supports relative paths. |
| 15 | + |
| 16 | +```tsx |
| 17 | +import { A } from "@solidjs/router"; |
| 18 | + |
| 19 | +function DashboardPage() { |
| 20 | + return ( |
| 21 | + <main> |
| 22 | + <nav> |
| 23 | + <A href="/">Home</a> |
| 24 | + </nav> |
| 25 | + {/* This is a relative path that, from /dashboard, links to /dashboard/users */} |
| 26 | + <A href="users">Users</A> |
| 27 | + </main> |
| 28 | + ); |
| 29 | +} |
36 | 30 | ``` |
37 | 31 |
|
38 | | -### Styling links |
| 32 | +See the [`<A>` API reference](/solid-router/reference/components/a) for more information. |
| 33 | + |
| 34 | +### Styling |
| 35 | + |
| 36 | +The `<A>` component allows you to style links based on their active state using the `activeClass` and `inactiveClass` props. |
| 37 | +When provided, these props apply the corresponding CSS classes to the link. |
| 38 | +If omitted, the default classes `active` and `inactive` are used. |
| 39 | + |
| 40 | +By default, a link is considered active when the current route matches the link's `href` or any of its descendant routes. |
| 41 | +For example, a link with `href="/dashboard"` is active when the current route is `/dashboard`, `/dashboard/users`, or `/dashboard/users/1/profile`. |
| 42 | + |
| 43 | +To match an exact route, the prop `end` can be used. |
| 44 | +When `true`, it ensures that the link is only considered active if the `href` exactly matches the current route. |
| 45 | +This is useful for root route links (href="/"), which might otherwise match all routes. |
39 | 46 |
|
40 | | -In addition, the `<A>` component also provides an `activeClass` and `inactiveClass` prop. |
41 | | -This prop accepts a string that, when provided, will apply the specified class to the anchor tag based on the current route. |
42 | | -If the current route matches the `href` prop, the `activeClass` class will be applied, otherwise the `inactiveClass` class will be applied. |
| 47 | +```tsx |
| 48 | +import { A } from "@solidjs/router"; |
43 | 49 |
|
44 | | -```jsx |
45 | | -<A href="/users" activeClass="underlined" inactiveClass="default"> |
46 | | - Users |
47 | | -</A> |
| 50 | +function Navbar() { |
| 51 | + return ( |
| 52 | + <nav> |
| 53 | + <A href="/" end={true}> |
| 54 | + Home |
| 55 | + </A> |
| 56 | + <A |
| 57 | + href="/login" |
| 58 | + activeClass="text-blue-900" |
| 59 | + inactiveClass="text-blue-500" |
| 60 | + > |
| 61 | + Login |
| 62 | + </A> |
| 63 | + </nav> |
| 64 | + ); |
| 65 | +} |
48 | 66 | ``` |
49 | 67 |
|
50 | | -### Matching routes |
| 68 | +## `useNavigate` primitive |
| 69 | + |
| 70 | +The `useNavigate` primitive allows for programmatically navigating to a specified route. |
51 | 71 |
|
52 | | -By default, matching using the `<A>` component will include locations that are _descendants_ of the specified route. |
53 | | -This would include `/users`, `/users/1`, `/users/1/posts`, etc. if `/users` is the specified route. |
| 72 | +```tsx |
| 73 | +import { useNavigate } from "@solidjs/router"; |
54 | 74 |
|
55 | | -```jsx |
56 | | -// Will match /users and /users/1 |
57 | | -<A href="/users"> |
58 | | - Users |
59 | | -</A> |
| 75 | +function LoginPage() { |
| 76 | + const navigate = useNavigate(); |
| 77 | + |
| 78 | + return ( |
| 79 | + <button |
| 80 | + onClick={() => { |
| 81 | + // Login logic |
| 82 | + navigate("/dashboard", { replace: true }); |
| 83 | + }} |
| 84 | + > |
| 85 | + Login |
| 86 | + </button> |
| 87 | + ); |
| 88 | +} |
60 | 89 | ``` |
61 | | -To match the exact route only, you can use the `end` prop: |
62 | 90 |
|
63 | | -```jsx |
64 | | -// Will match /users/1 only |
65 | | -<A href="/users/1" end> |
66 | | - User 1 |
67 | | -</A> |
| 91 | +This example redirects the user to `/dashboard` after login. |
| 92 | +By using `replace: true`, the login page is removed from the browser's history stack and replaced with the `/dashboard` route. |
| 93 | +This prevents the user from navigating back to the login page using the back button. |
| 94 | + |
| 95 | +See the [`useNavigate` API reference](/solid-router/reference/primitives/use-navigate) for more information. |
| 96 | + |
| 97 | +## `redirect` function |
| 98 | + |
| 99 | +The `redirect` function returns a [`Response` object](https://developer.mozilla.org/en-US/docs/Web/API/Response), which enables navigation to a specified route within [query](/solid-router/reference/data-apis/query) or [action](/solid-router/reference/data-apis/action). |
| 100 | + |
| 101 | +```tsx |
| 102 | +import { action, redirect } from "@solidjs/router"; |
| 103 | + |
| 104 | +const logout = action(async () => { |
| 105 | + localStorage.remove("token"); |
| 106 | + throw redirect("/"); |
| 107 | +}); |
68 | 108 | ``` |
69 | 109 |
|
| 110 | +See the [`redirect` API reference](/solid-router/reference/response-helpers/redirect) for more information. |
0 commit comments