@solidjs/router 0.4.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +557 -0
- package/dist/components.d.ts +65 -0
- package/dist/components.jsx +111 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.js +936 -0
- package/dist/index.jsx +4 -0
- package/dist/integration.d.ts +6 -0
- package/dist/integration.js +105 -0
- package/dist/routing.d.ts +24 -0
- package/dist/routing.js +377 -0
- package/dist/types.d.ts +115 -0
- package/dist/types.js +1 -0
- package/dist/utils.d.ts +11 -0
- package/dist/utils.js +132 -0
- package/package.json +62 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2020-2022 Ryan Carniato
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,557 @@
|
|
|
1
|
+
<p>
|
|
2
|
+
<img src="https://assets.solidjs.com/banner?project=Router&type=core" alt="Solid Router" />
|
|
3
|
+
</p>
|
|
4
|
+
|
|
5
|
+
> 0.3.x only works with Solid v1.3.5 or later.
|
|
6
|
+
> `useData` has been renamed to `useRouteData` and no longer takes arguments. Refer to documentation below.
|
|
7
|
+
|
|
8
|
+
# Solid Router [](https://www.npmjs.org/package/@solidjs/router)
|
|
9
|
+
|
|
10
|
+
A router lets you change your view based on the URL in the browser. This allows your "single-page" application to simulate a traditional multipage site. To use Solid Router, you specify components called Routes that depend on the value of the URL (the "path"), and the router handles the mechanism of swapping them in and out.
|
|
11
|
+
|
|
12
|
+
Solid Router is a universal router for SolidJS - it works whether you're rendering on the client or on the server. It was inspired by and combines paradigms of React Router and the Ember Router. Routes can be defined directly in your app's template using JSX, but you can also pass your route configuration directly as an object. It also supports nested routing, so navigation can change a part of a component, rather than completely replacing it.
|
|
13
|
+
|
|
14
|
+
It supports all of Solid's SSR methods and has Solid's transitions baked in, so use it freely with suspense, resources, and lazy components. Solid Router also allows you to define a data function that loads parallel to the routes ([render-as-you-fetch](https://epicreact.dev/render-as-you-fetch/)).
|
|
15
|
+
|
|
16
|
+
- [Getting Started](#getting-started)
|
|
17
|
+
- [Set Up the Router](#set-up-the-router)
|
|
18
|
+
- [Configure Your Routes](#configure-your-routes)
|
|
19
|
+
- [Create Links to Your Routes](#create-links-to-your-routes)
|
|
20
|
+
- [Dynamic Routes](#dynamic-routes)
|
|
21
|
+
- [Data Functions](#data-functions)
|
|
22
|
+
- [Nested Routes](#nested-routes)
|
|
23
|
+
- [Hash Mode Router](#hash-mode-router)
|
|
24
|
+
- [Config Based Routing](#config-based-routing)
|
|
25
|
+
- [Router Primitives](#router-primitives)
|
|
26
|
+
- [useParams](#useparams)
|
|
27
|
+
- [useNavigate](#usenavigate)
|
|
28
|
+
- [useLocation](#uselocation)
|
|
29
|
+
- [useSearchParams](#usesearchparams)
|
|
30
|
+
- [useIsRouting](#useisrouting)
|
|
31
|
+
- [useRouteData](#useroutedata)
|
|
32
|
+
- [useMatch](#usematch)
|
|
33
|
+
- [useRoutes](#useroutes)
|
|
34
|
+
|
|
35
|
+
## Getting Started
|
|
36
|
+
|
|
37
|
+
### Set Up the Router
|
|
38
|
+
|
|
39
|
+
```sh
|
|
40
|
+
> npm i @solidjs/router
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
Install `@solidjs/router`, then wrap your root component with the Router component:
|
|
44
|
+
|
|
45
|
+
```jsx
|
|
46
|
+
import { render } from "solid-js/web";
|
|
47
|
+
import { Router } from "@solidjs/router";
|
|
48
|
+
import App from "./App";
|
|
49
|
+
|
|
50
|
+
render(
|
|
51
|
+
() => (
|
|
52
|
+
<Router>
|
|
53
|
+
<App />
|
|
54
|
+
</Router>
|
|
55
|
+
),
|
|
56
|
+
document.getElementById("app")
|
|
57
|
+
);
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
This sets up a context so that we can display the routes anywhere in the app.
|
|
61
|
+
|
|
62
|
+
### Configure Your Routes
|
|
63
|
+
|
|
64
|
+
Solid Router allows you to configure your routes using JSX:
|
|
65
|
+
|
|
66
|
+
1. Use the `Routes` component to specify where the routes should appear in your app.
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
```jsx
|
|
70
|
+
import { Routes, Route } from "@solidjs/router"
|
|
71
|
+
|
|
72
|
+
export default function App() {
|
|
73
|
+
return <>
|
|
74
|
+
<h1>My Site with Lots of Pages</h1>
|
|
75
|
+
<Routes>
|
|
76
|
+
|
|
77
|
+
</Routes>
|
|
78
|
+
</>
|
|
79
|
+
}
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
2. Add each route using the `Route` component, specifying a path and an element or component to render when the user navigates to that path.
|
|
83
|
+
|
|
84
|
+
```jsx
|
|
85
|
+
import { Routes, Route } from "@solidjs/router"
|
|
86
|
+
|
|
87
|
+
import Home from "./pages/Home"
|
|
88
|
+
import Users from "./pages/Users"
|
|
89
|
+
|
|
90
|
+
export default function App() {
|
|
91
|
+
return <>
|
|
92
|
+
<h1>My Site with Lots of Pages</h1>
|
|
93
|
+
<Routes>
|
|
94
|
+
<Route path="/users" component={Users} />
|
|
95
|
+
<Route path="/" component={Home} />
|
|
96
|
+
<Route path="/about" element={<div>This site was made with Solid</div>} />
|
|
97
|
+
</Routes>
|
|
98
|
+
</>
|
|
99
|
+
}
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
3. Lazy-load route components
|
|
103
|
+
|
|
104
|
+
This way, the `Users` and `Home` components will only be loaded if you're navigating to `/users` or `/home`, respectively.
|
|
105
|
+
|
|
106
|
+
```jsx
|
|
107
|
+
import { lazy } from "solid-js";
|
|
108
|
+
import { Routes, Route } from "@solidjs/router"
|
|
109
|
+
const Users = lazy(() => import("./pages/Users"));
|
|
110
|
+
const Home = lazy(() => import("./pages/Home"));
|
|
111
|
+
|
|
112
|
+
export default function App() {
|
|
113
|
+
return <>
|
|
114
|
+
<h1>My Site with Lots of Pages</h1>
|
|
115
|
+
<Routes>
|
|
116
|
+
<Route path="/users" component={Users} />
|
|
117
|
+
<Route path="/" component={Home} />
|
|
118
|
+
<Route path="/about" element={<div>This site was made with Solid</div>} />
|
|
119
|
+
</Routes>
|
|
120
|
+
</>
|
|
121
|
+
}
|
|
122
|
+
```
|
|
123
|
+
|
|
124
|
+
## Create Links to Your Routes
|
|
125
|
+
|
|
126
|
+
Use the `Link` component to create an anchor tag that takes you to a route:
|
|
127
|
+
|
|
128
|
+
```jsx
|
|
129
|
+
import { lazy } from "solid-js";
|
|
130
|
+
import { Routes, Route, Link } from "@solidjs/router"
|
|
131
|
+
const Users = lazy(() => import("./pages/Users"));
|
|
132
|
+
const Home = lazy(() => import("./pages/Home"));
|
|
133
|
+
|
|
134
|
+
export default function App() {
|
|
135
|
+
return <>
|
|
136
|
+
<h1>My Site with Lots of Pages</h1>
|
|
137
|
+
<nav>
|
|
138
|
+
<Link href="/about">About</Link>
|
|
139
|
+
<Link href="/">Home</Link>
|
|
140
|
+
</nav>
|
|
141
|
+
<Routes>
|
|
142
|
+
<Route path="/users" component={Users} />
|
|
143
|
+
<Route path="/" component={Home} />
|
|
144
|
+
<Route path="/about" element={<div>This site was made with Solid</div>} />
|
|
145
|
+
</Routes>
|
|
146
|
+
</>
|
|
147
|
+
}
|
|
148
|
+
```
|
|
149
|
+
|
|
150
|
+
If you use `NavLink` instead of `Link`, the anchor tag will have an `active` class if its href matches the current location, and `inactive` otherwise. **Note:** By default matching includes locations that are descendents (eg. href `/users` matches locations `/users` and `/users/123`), use the boolean `end` prop to prevent matching these. This is particularly useful for links to the root route `/` which would match everything.
|
|
151
|
+
|
|
152
|
+
Both of these components have these props:
|
|
153
|
+
|
|
154
|
+
| prop | type | description |
|
|
155
|
+
|----------|---------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
|
|
156
|
+
| href | string | The path of the route to navigate to. This will be resolved relative to the route that the link is in, but you can preface it with `/` to refer back to the root. |
|
|
157
|
+
| noScroll | boolean | If true, turn off the default behavior of scrolling to the top of the new page |
|
|
158
|
+
| replace | boolean | If true, don't add a new entry to the browser history. (By default, the new page will be added to the browser history, so pressing the back button will take you to the previous route.) |
|
|
159
|
+
| state | unknown | [Push this value](https://developer.mozilla.org/en-US/docs/Web/API/History/pushState) to the history stack when navigating |
|
|
160
|
+
|
|
161
|
+
|
|
162
|
+
`NavLink` additionally has:
|
|
163
|
+
|
|
164
|
+
| prop | type | description |
|
|
165
|
+
|----------|---------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
|
|
166
|
+
| inactiveClass | string | The class to show when the link is inactive (when the current location doesn't match the link) |
|
|
167
|
+
| activeClass | string | The class to show when the link is active |
|
|
168
|
+
| end | boolean | If `true`, only considers the link to be active when the curent location matches the `href` exactly; if `false`, check if the current location _starts with_ `href` |
|
|
169
|
+
|
|
170
|
+
|
|
171
|
+
|
|
172
|
+
|
|
173
|
+
If you have a same-domain path that you want to link to _without_ going through the router, set `rel="external"` on the link component.
|
|
174
|
+
|
|
175
|
+
### The Navigate Component
|
|
176
|
+
Solid Router provides a `Navigate` component that works similarly to `Link` and `NavLink`, but it will _immediately_ navigate to the provided path as soon as the component is rendered. It also uses the `href` prop, but you have the additional option of passing a function to `href` that returns a path to navigate to:
|
|
177
|
+
|
|
178
|
+
```jsx
|
|
179
|
+
function getPath ({navigate, location}) {
|
|
180
|
+
//navigate is the result of calling useNavigate(); location is the result of calling useLocation().
|
|
181
|
+
//You can use those to dynamically determine a path to navigate to
|
|
182
|
+
return "/some-path";
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
//Navigating to /redirect will redirect you to the result of getPath
|
|
186
|
+
<Route path="/redirect">
|
|
187
|
+
<Navigate href={getPath}/>
|
|
188
|
+
</Route>
|
|
189
|
+
```
|
|
190
|
+
|
|
191
|
+
## Dynamic Routes
|
|
192
|
+
|
|
193
|
+
If you don't know the path ahead of time, you might want to treat part of the path as a flexible parameter that is passed on to the component.
|
|
194
|
+
|
|
195
|
+
```jsx
|
|
196
|
+
import { lazy } from "solid-js";
|
|
197
|
+
import { Routes, Route } from "@solidjs/router"
|
|
198
|
+
const Users = lazy(() => import("./pages/Users"));
|
|
199
|
+
const User = lazy(() => import("./pages/User"));
|
|
200
|
+
const Home = lazy(() => import("./pages/Home"));
|
|
201
|
+
|
|
202
|
+
export default function App() {
|
|
203
|
+
return <>
|
|
204
|
+
<h1>My Site with Lots of Pages<h1/>
|
|
205
|
+
<Routes>
|
|
206
|
+
<Route path="/users" component={Users} />
|
|
207
|
+
<Route path="/users/:id" component={User} />
|
|
208
|
+
<Route path="/" component={Home} />
|
|
209
|
+
<Route path="/about" element={<div>This site was made with Solid</div>} />
|
|
210
|
+
</Routes>
|
|
211
|
+
</>
|
|
212
|
+
}
|
|
213
|
+
```
|
|
214
|
+
|
|
215
|
+
The colon indicates that `id` can be any string, and as long as the URL fits that pattern, the `User` component will show.
|
|
216
|
+
|
|
217
|
+
You can then access that `id` from within a route component with `useParams`:
|
|
218
|
+
|
|
219
|
+
|
|
220
|
+
```jsx
|
|
221
|
+
//async fetching function
|
|
222
|
+
import { fetchUser } ...
|
|
223
|
+
|
|
224
|
+
export default function User () {
|
|
225
|
+
|
|
226
|
+
const params = useParams();
|
|
227
|
+
|
|
228
|
+
const [userData] = createResource(() => params.id, fetchUser);
|
|
229
|
+
|
|
230
|
+
return <a href={userData.twitter}>{userData.name}</a>
|
|
231
|
+
}
|
|
232
|
+
```
|
|
233
|
+
|
|
234
|
+
### Optional Parameters
|
|
235
|
+
|
|
236
|
+
Parameters can be specified as optional by adding a question mark to the end of the parameter name:
|
|
237
|
+
|
|
238
|
+
```jsx
|
|
239
|
+
//Matches stories and stories/123 but not stories/123/comments
|
|
240
|
+
<Route path='/stories/:id?' element={<Stories/>} />
|
|
241
|
+
```
|
|
242
|
+
|
|
243
|
+
### Wildcard Routes
|
|
244
|
+
|
|
245
|
+
`:param` lets you match an arbitrary name at that point in the path. You can use `*` to match any end of the path:
|
|
246
|
+
|
|
247
|
+
```jsx
|
|
248
|
+
//Matches any path that begins with foo, including foo/, foo/a/, foo/a/b/c
|
|
249
|
+
<Route path='foo/*' component={Foo}/>
|
|
250
|
+
```
|
|
251
|
+
|
|
252
|
+
If you want to expose the wild part of the path to the component as a parameter, you can name it:
|
|
253
|
+
|
|
254
|
+
```jsx
|
|
255
|
+
<Route path='foo/*any' element={<div>{useParams().any}</div>}/>
|
|
256
|
+
```
|
|
257
|
+
|
|
258
|
+
Note that the wildcard token must be the last part of the path; `foo/*any/bar` won't create any routes.
|
|
259
|
+
|
|
260
|
+
### Multiple Paths
|
|
261
|
+
|
|
262
|
+
Routes also support defining multiple paths using an array. This allows a route to remain mounted and not rerender when switching between two or more locations that it matches:
|
|
263
|
+
|
|
264
|
+
```jsx
|
|
265
|
+
//Navigating from login to register does not cause the Login component to re-render
|
|
266
|
+
<Route path={["login", "register"]} component={Login}/>
|
|
267
|
+
```
|
|
268
|
+
|
|
269
|
+
|
|
270
|
+
## Data Functions
|
|
271
|
+
In the [above example](#dynamic-routes), the User component is lazy-loaded and then the data is fetched. With route data functions, we can instead start fetching the data parallel to loading the route, so we can use the data as soon as possible.
|
|
272
|
+
|
|
273
|
+
To do this, create a function that fetches and returns the data using `createResource`. Then pass that function to the `data` prop of the `Route` component.
|
|
274
|
+
|
|
275
|
+
|
|
276
|
+
```js
|
|
277
|
+
import { lazy } from "solid-js";
|
|
278
|
+
import { Route } from "@solidjs/router";
|
|
279
|
+
import { fetchUser } ...
|
|
280
|
+
|
|
281
|
+
const User = lazy(() => import("/pages/users/[id].js"));
|
|
282
|
+
|
|
283
|
+
//Data function
|
|
284
|
+
function UserData({params, location, navigate, data}) {
|
|
285
|
+
const [user] = createResource(() => params.id, fetchUser);
|
|
286
|
+
return user;
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
//Pass it in the route definition
|
|
290
|
+
<Route path="/users/:id" component={User} data={UserData} />;
|
|
291
|
+
```
|
|
292
|
+
|
|
293
|
+
When the route is loaded, the data function is called, and the result can be accessed by calling `useRouteData()` in the route component.
|
|
294
|
+
|
|
295
|
+
```jsx
|
|
296
|
+
//pages/users/[id].js
|
|
297
|
+
import { useRouteData } from '@solidjs/router';
|
|
298
|
+
export default function User() {
|
|
299
|
+
const user = useRouteData();
|
|
300
|
+
return <h1>{user().name}</h1>;
|
|
301
|
+
}
|
|
302
|
+
```
|
|
303
|
+
|
|
304
|
+
As its only argument, the data function is passed an object that you can use to access route information:
|
|
305
|
+
|
|
306
|
+
| key | type | description |
|
|
307
|
+
|-----------|------------------------------------------------|-------------------------------------------------------------------------------------------------------------|
|
|
308
|
+
| params | object | The route parameters (same value as calling `useParams()` inside the route component) |
|
|
309
|
+
| location | `{ pathname, search, hash, query, state, key}` | An object that you can use to get more information about the path (corresponds to [`useLocation()`](#uselocation)) |
|
|
310
|
+
| navigate | `(to: string, options?: NavigateOptions) => void` | A function that you can call to navigate to a different route instead (corresponds to [`useNavigate()`](#usenavigate)) |
|
|
311
|
+
| data | unknown | The data returned by the [parent's](#nested-routes) data function, if any. (Data will pass through any intermediate nesting.) |
|
|
312
|
+
|
|
313
|
+
A common pattern is to export the data function that corresponds to a route in a dedicated `route.data.js` file. This way, the data function can be imported without loading anything else.
|
|
314
|
+
|
|
315
|
+
```js
|
|
316
|
+
import { lazy } from "solid-js";
|
|
317
|
+
import { Route } from "@solidjs/router";
|
|
318
|
+
import { fetchUser } ...
|
|
319
|
+
import UserData from "./pages/users/[id].data.js";
|
|
320
|
+
const User = lazy(() => import("/pages/users/[id].js"));
|
|
321
|
+
|
|
322
|
+
// In the Route definition
|
|
323
|
+
<Route path="/users/:id" component={User} data={UserData} />;
|
|
324
|
+
```
|
|
325
|
+
|
|
326
|
+
## Nested Routes
|
|
327
|
+
The following two route definitions have the same result:
|
|
328
|
+
|
|
329
|
+
```jsx
|
|
330
|
+
<Route path="/users/:id" component={User} />
|
|
331
|
+
```
|
|
332
|
+
```jsx
|
|
333
|
+
<Route path="/users">
|
|
334
|
+
<Route path="/:id" component={User} />
|
|
335
|
+
</Route>
|
|
336
|
+
```
|
|
337
|
+
`/users/:id` renders the `<User/>` component, and `/users/` is an empty route.
|
|
338
|
+
|
|
339
|
+
Only leaf Route nodes (innermost `Route` components) are given a route. If you want to make the parent its own route, you have to specify it separately:
|
|
340
|
+
|
|
341
|
+
```jsx
|
|
342
|
+
//This won't work the way you'd expect
|
|
343
|
+
<Route path="/users" component={Users}>
|
|
344
|
+
<Route path="/:id" component={User} />
|
|
345
|
+
</Route>
|
|
346
|
+
|
|
347
|
+
//This works
|
|
348
|
+
<Route path="/users" component={Users} />
|
|
349
|
+
<Route path="/users/:id" component={User} />
|
|
350
|
+
|
|
351
|
+
//This also works
|
|
352
|
+
<Route path="/users">
|
|
353
|
+
<Route path="/" component={Users} />
|
|
354
|
+
<Route path="/:id" component={User} />
|
|
355
|
+
</Route>
|
|
356
|
+
```
|
|
357
|
+
|
|
358
|
+
You can also take advantage of nesting by adding a parent element with an `<Outlet/>`.
|
|
359
|
+
```jsx
|
|
360
|
+
|
|
361
|
+
import { Outlet } from "@solidjs/router";
|
|
362
|
+
|
|
363
|
+
function PageWrapper () {
|
|
364
|
+
return <div>
|
|
365
|
+
<h1> We love our users! </h1>
|
|
366
|
+
<Outlet/>
|
|
367
|
+
<Link href="/">Back Home</Link>
|
|
368
|
+
</div>
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
<Route path="/users" component={PageWrapper}>
|
|
372
|
+
<Route path="/" component={Users}/>
|
|
373
|
+
<Route path="/:id" component={User} />
|
|
374
|
+
</Route>
|
|
375
|
+
```
|
|
376
|
+
The routes are still configured the same, but now the route elements will appear inside the parent element where the `<Outlet/>` was declared.
|
|
377
|
+
|
|
378
|
+
You can nest indefinitely - just remember that only leaf nodes will become their own routes. In this example, the only route created is `/layer1/layer2`, and it appears as three nested divs.
|
|
379
|
+
|
|
380
|
+
```jsx
|
|
381
|
+
<Route path='/' element={<div>Onion starts here <Outlet /></div>}>
|
|
382
|
+
<Route path='layer1' element={<div>Another layer <Outlet /></div>}>
|
|
383
|
+
<Route path='layer2' element={<div>Innermost layer</div>}></Route>
|
|
384
|
+
</Route>
|
|
385
|
+
</Route>
|
|
386
|
+
```
|
|
387
|
+
|
|
388
|
+
If you declare a `data` function on a parent and a child, the result of the parent's data function will be passed to the child's data function as the `data` property of the argument, as described in the last section. This works even if it isn't a direct child, because by default every route forwards its parent's data.
|
|
389
|
+
|
|
390
|
+
## Hash Mode Router
|
|
391
|
+
|
|
392
|
+
By default, Solid Router uses `location.pathname` as route path. You can simply switch to hash mode through the `source` property on `<Router>` component.
|
|
393
|
+
|
|
394
|
+
```jsx
|
|
395
|
+
import { Router, hashIntegration } from '@solidjs/router'
|
|
396
|
+
|
|
397
|
+
<Router source={hashIntegration()}><App></Router>
|
|
398
|
+
```
|
|
399
|
+
|
|
400
|
+
## Config Based Routing
|
|
401
|
+
|
|
402
|
+
You don't have to use JSX to set up your routes; you can pass an object directly with `useRoutes`:
|
|
403
|
+
|
|
404
|
+
```jsx
|
|
405
|
+
import { lazy } from "solid-js";
|
|
406
|
+
import { render } from "solid-js/web";
|
|
407
|
+
import { Router, useRoutes, Link } from "@solidjs/router";
|
|
408
|
+
|
|
409
|
+
const routes = [
|
|
410
|
+
{
|
|
411
|
+
path: "/users",
|
|
412
|
+
component: lazy(() => import("/pages/users.js"))
|
|
413
|
+
},
|
|
414
|
+
{
|
|
415
|
+
path: "/users/:id",
|
|
416
|
+
component: lazy(() => import("/pages/users/[id].js")),
|
|
417
|
+
children: [
|
|
418
|
+
{ path: "/", component: lazy(() => import("/pages/users/[id]/index.js")) },
|
|
419
|
+
{ path: "/settings", component: lazy(() => import("/pages/users/[id]/settings.js")) },
|
|
420
|
+
{ path: "/*all", component: lazy(() => import("/pages/users/[id]/[...all].js")) }
|
|
421
|
+
]
|
|
422
|
+
},
|
|
423
|
+
{
|
|
424
|
+
path: "/",
|
|
425
|
+
component: lazy(() => import("/pages/index.js"))
|
|
426
|
+
},
|
|
427
|
+
{
|
|
428
|
+
path: "/*all",
|
|
429
|
+
component: lazy(() => import("/pages/[...all].js"))
|
|
430
|
+
}
|
|
431
|
+
];
|
|
432
|
+
|
|
433
|
+
function App() {
|
|
434
|
+
const Routes = useRoutes(routes);
|
|
435
|
+
return (
|
|
436
|
+
<>
|
|
437
|
+
<h1>Awesome Site</h1>
|
|
438
|
+
<Link class="nav" href="/">
|
|
439
|
+
Home
|
|
440
|
+
</Link>
|
|
441
|
+
<Link class="nav" href="/users">
|
|
442
|
+
Users
|
|
443
|
+
</Link>
|
|
444
|
+
<Routes />
|
|
445
|
+
</>
|
|
446
|
+
);
|
|
447
|
+
}
|
|
448
|
+
|
|
449
|
+
render(
|
|
450
|
+
() => (
|
|
451
|
+
<Router>
|
|
452
|
+
<App />
|
|
453
|
+
</Router>
|
|
454
|
+
),
|
|
455
|
+
document.getElementById("app")
|
|
456
|
+
);
|
|
457
|
+
```
|
|
458
|
+
## Router Primitives
|
|
459
|
+
|
|
460
|
+
Solid Router provides a number of primitives that read off the Router and Route context.
|
|
461
|
+
|
|
462
|
+
### useParams
|
|
463
|
+
|
|
464
|
+
Retrieves a reactive, store-like object containing the current route path parameters as defined in the Route.
|
|
465
|
+
|
|
466
|
+
```js
|
|
467
|
+
const params = useParams();
|
|
468
|
+
|
|
469
|
+
// fetch user based on the id path parameter
|
|
470
|
+
const [user] = createResource(() => params.id, fetchUser);
|
|
471
|
+
```
|
|
472
|
+
|
|
473
|
+
### useNavigate
|
|
474
|
+
|
|
475
|
+
Retrieves method to do navigation. The method accepts a path to navigate to and an optional object with the following options:
|
|
476
|
+
|
|
477
|
+
- resolve (_boolean_, default `true`): resolve the path against the current route
|
|
478
|
+
- replace (_boolean_, default `false`): replace the history entry
|
|
479
|
+
- scroll (_boolean_, default `true`): scroll to top after navigation
|
|
480
|
+
- state (_any_, default `undefined`): pass custom state to `location.state`
|
|
481
|
+
|
|
482
|
+
__Note:__ The state is serialized using the [structured clone algorithm](https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Structured_clone_algorithm) which does not support all object types.
|
|
483
|
+
|
|
484
|
+
```js
|
|
485
|
+
const navigate = useNavigate();
|
|
486
|
+
|
|
487
|
+
if (unauthorized) {
|
|
488
|
+
navigate("/login", { replace: true });
|
|
489
|
+
}
|
|
490
|
+
```
|
|
491
|
+
|
|
492
|
+
### useLocation
|
|
493
|
+
|
|
494
|
+
Retrieves reactive `location` object useful for getting things like `pathname`
|
|
495
|
+
|
|
496
|
+
```js
|
|
497
|
+
const location = useLocation();
|
|
498
|
+
|
|
499
|
+
const pathname = createMemo(() => parsePath(location.pathname));
|
|
500
|
+
```
|
|
501
|
+
|
|
502
|
+
### useSearchParams
|
|
503
|
+
|
|
504
|
+
Retrieves a tuple containing a reactive object to read the current location's query parameters and a method to update them. The object is a proxy so you must access properties to subscribe to reactive updates. Note values will be strings and property names will retain their casing.
|
|
505
|
+
|
|
506
|
+
The setter method accepts an object whose entries will be merged into the current query string. Values `''`, `undefined` and `null` will remove the key from the resulting query string. Updates will behave just like a navigation and the setter accepts the same optional second parameter as `navigate` and auto-scrolling is disabled by default.
|
|
507
|
+
|
|
508
|
+
```js
|
|
509
|
+
const [searchParams, setSearchParams] = useSearchParams();
|
|
510
|
+
|
|
511
|
+
return (
|
|
512
|
+
<div>
|
|
513
|
+
<span>Page: {searchParams.page}</span>
|
|
514
|
+
<button onClick={() => setSearchParams({ page: searchParams.page + 1 })}>Next Page</button>
|
|
515
|
+
</div>
|
|
516
|
+
);
|
|
517
|
+
```
|
|
518
|
+
|
|
519
|
+
### useIsRouting
|
|
520
|
+
|
|
521
|
+
Retrieves signal that indicates whether the route is currently in a Transition. Useful for showing stale/pending state when the route resolution is Suspended during concurrent rendering.
|
|
522
|
+
|
|
523
|
+
```js
|
|
524
|
+
const isRouting = useIsRouting();
|
|
525
|
+
|
|
526
|
+
return (
|
|
527
|
+
<div classList={{ "grey-out": isRouting() }}>
|
|
528
|
+
<MyAwesomeConent />
|
|
529
|
+
</div>
|
|
530
|
+
);
|
|
531
|
+
```
|
|
532
|
+
|
|
533
|
+
### useRouteData
|
|
534
|
+
|
|
535
|
+
Retrieves the return value from the data function.
|
|
536
|
+
|
|
537
|
+
> In previous versions you could use numbers to access parent data. This is no longer supported. Instead the data functions themselves receive the parent data that you can expose through the specific nested routes data.
|
|
538
|
+
|
|
539
|
+
```js
|
|
540
|
+
const user = useRouteData();
|
|
541
|
+
|
|
542
|
+
return <h1>{user().name}</h1>;
|
|
543
|
+
```
|
|
544
|
+
|
|
545
|
+
### useMatch
|
|
546
|
+
|
|
547
|
+
`useMatch` takes an accessor that returns the path and creates a Memo that returns match information if the current path matches the provided path. Useful for determining if a given path matches the current route.
|
|
548
|
+
|
|
549
|
+
```js
|
|
550
|
+
const match = useMatch(() => props.href);
|
|
551
|
+
|
|
552
|
+
return <div classList={{ active: Boolean(match()) }} />;
|
|
553
|
+
```
|
|
554
|
+
|
|
555
|
+
### useRoutes
|
|
556
|
+
|
|
557
|
+
Used to define routes via a config object instead of JSX. See [Config Based Routing](#config-based-routing).
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import type { Component, JSX } from "solid-js";
|
|
2
|
+
import type { Location, LocationChangeSignal, Navigator, RouteDataFunc, RouteDefinition, RouterIntegration } from "./types";
|
|
3
|
+
declare module "solid-js" {
|
|
4
|
+
namespace JSX {
|
|
5
|
+
interface AnchorHTMLAttributes<T> {
|
|
6
|
+
state?: string;
|
|
7
|
+
noScroll?: boolean;
|
|
8
|
+
replace?: boolean;
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
export declare type RouterProps = {
|
|
13
|
+
base?: string;
|
|
14
|
+
data?: RouteDataFunc;
|
|
15
|
+
children: JSX.Element;
|
|
16
|
+
out?: object;
|
|
17
|
+
} & ({
|
|
18
|
+
url?: never;
|
|
19
|
+
source?: RouterIntegration | LocationChangeSignal;
|
|
20
|
+
} | {
|
|
21
|
+
source?: never;
|
|
22
|
+
url: string;
|
|
23
|
+
});
|
|
24
|
+
export declare const Router: (props: RouterProps) => JSX.Element;
|
|
25
|
+
export interface RoutesProps {
|
|
26
|
+
base?: string;
|
|
27
|
+
children: JSX.Element;
|
|
28
|
+
}
|
|
29
|
+
export declare const Routes: (props: RoutesProps) => JSX.Element;
|
|
30
|
+
export declare const useRoutes: (routes: RouteDefinition | RouteDefinition[], base?: string | undefined) => () => JSX.Element;
|
|
31
|
+
export declare type RouteProps = {
|
|
32
|
+
path: string | string[];
|
|
33
|
+
children?: JSX.Element;
|
|
34
|
+
data?: RouteDataFunc;
|
|
35
|
+
} & ({
|
|
36
|
+
element?: never;
|
|
37
|
+
component: Component;
|
|
38
|
+
} | {
|
|
39
|
+
component?: never;
|
|
40
|
+
element?: JSX.Element;
|
|
41
|
+
preload?: () => void;
|
|
42
|
+
});
|
|
43
|
+
export declare const Route: (props: RouteProps) => JSX.Element;
|
|
44
|
+
export declare const Outlet: () => JSX.Element;
|
|
45
|
+
export interface LinkProps extends Omit<JSX.AnchorHTMLAttributes<HTMLAnchorElement>, "state"> {
|
|
46
|
+
href: string;
|
|
47
|
+
replace?: boolean;
|
|
48
|
+
noScroll?: boolean;
|
|
49
|
+
state?: unknown;
|
|
50
|
+
}
|
|
51
|
+
export declare function Link(props: LinkProps): JSX.Element;
|
|
52
|
+
export interface NavLinkProps extends LinkProps {
|
|
53
|
+
inactiveClass?: string;
|
|
54
|
+
activeClass?: string;
|
|
55
|
+
end?: boolean;
|
|
56
|
+
}
|
|
57
|
+
export declare function NavLink(props: NavLinkProps): JSX.Element;
|
|
58
|
+
export interface NavigateProps {
|
|
59
|
+
href: ((args: {
|
|
60
|
+
navigate: Navigator;
|
|
61
|
+
location: Location;
|
|
62
|
+
}) => string) | string;
|
|
63
|
+
state?: unknown;
|
|
64
|
+
}
|
|
65
|
+
export declare function Navigate(props: NavigateProps): null;
|