@riab/cpm-archiving 1.1.2-beta-4 → 1.1.2-beta.6
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/README.md +481 -481
- package/dist/cpm-archiving.js +94 -94
- package/dist/index.html +33 -33
- package/package.json +69 -69
package/README.md
CHANGED
|
@@ -1,481 +1,481 @@
|
|
|
1
|
-
Welcome to your new TanStack app!
|
|
2
|
-
|
|
3
|
-
# Getting Started
|
|
4
|
-
|
|
5
|
-
To run this application:
|
|
6
|
-
|
|
7
|
-
```bash
|
|
8
|
-
npm install
|
|
9
|
-
npm run start
|
|
10
|
-
```
|
|
11
|
-
|
|
12
|
-
# Building For Production
|
|
13
|
-
|
|
14
|
-
To build this application for production:
|
|
15
|
-
|
|
16
|
-
```bash
|
|
17
|
-
npm run build
|
|
18
|
-
```
|
|
19
|
-
|
|
20
|
-
## Testing
|
|
21
|
-
|
|
22
|
-
This project uses [Vitest](https://vitest.dev/) for testing. You can run the tests with:
|
|
23
|
-
|
|
24
|
-
```bash
|
|
25
|
-
npm run test
|
|
26
|
-
```
|
|
27
|
-
|
|
28
|
-
## Styling
|
|
29
|
-
|
|
30
|
-
This project uses [Tailwind CSS](https://tailwindcss.com/) for styling.
|
|
31
|
-
|
|
32
|
-
## Linting & Formatting
|
|
33
|
-
|
|
34
|
-
This project uses [eslint](https://eslint.org/) and [prettier](https://prettier.io/) for linting and formatting. Eslint is configured using [tanstack/eslint-config](https://tanstack.com/config/latest/docs/eslint). The following scripts are available:
|
|
35
|
-
|
|
36
|
-
```bash
|
|
37
|
-
npm run lint
|
|
38
|
-
npm run format
|
|
39
|
-
npm run check
|
|
40
|
-
```
|
|
41
|
-
|
|
42
|
-
## Routing
|
|
43
|
-
|
|
44
|
-
This project uses [TanStack Router](https://tanstack.com/router). The initial setup is a code based router. Which means that the routes are defined in code (in the `./src/main.tsx` file). If you like you can also use a file based routing setup by following the [File Based Routing](https://tanstack.com/router/latest/docs/framework/react/guide/file-based-routing) guide.
|
|
45
|
-
|
|
46
|
-
### Adding A Route
|
|
47
|
-
|
|
48
|
-
To add a new route to your application just add another `createRoute` call to the `./src/main.tsx` file. The example below adds a new `/about`route to the root route.
|
|
49
|
-
|
|
50
|
-
```tsx
|
|
51
|
-
const aboutRoute = createRoute({
|
|
52
|
-
getParentRoute: () => rootRoute,
|
|
53
|
-
path: '/about',
|
|
54
|
-
component: () => <h1>About</h1>,
|
|
55
|
-
})
|
|
56
|
-
```
|
|
57
|
-
|
|
58
|
-
You will also need to add the route to the `routeTree` in the `./src/main.tsx` file.
|
|
59
|
-
|
|
60
|
-
```tsx
|
|
61
|
-
const routeTree = rootRoute.addChildren([indexRoute, aboutRoute])
|
|
62
|
-
```
|
|
63
|
-
|
|
64
|
-
With this set up you should be able to navigate to `/about` and see the about page.
|
|
65
|
-
|
|
66
|
-
Of course you don't need to implement the About page in the `main.tsx` file. You can create that component in another file and import it into the `main.tsx` file, then use it in the `component` property of the `createRoute` call, like so:
|
|
67
|
-
|
|
68
|
-
```tsx
|
|
69
|
-
import About from './components/About.tsx'
|
|
70
|
-
|
|
71
|
-
const aboutRoute = createRoute({
|
|
72
|
-
getParentRoute: () => rootRoute,
|
|
73
|
-
path: '/about',
|
|
74
|
-
component: About,
|
|
75
|
-
})
|
|
76
|
-
```
|
|
77
|
-
|
|
78
|
-
That is how we have the `App` component set up with the home page.
|
|
79
|
-
|
|
80
|
-
For more information on the options you have when you are creating code based routes check out the [Code Based Routing](https://tanstack.com/router/latest/docs/framework/react/guide/code-based-routing) documentation.
|
|
81
|
-
|
|
82
|
-
Now that you have two routes you can use a `Link` component to navigate between them.
|
|
83
|
-
|
|
84
|
-
### Adding Links
|
|
85
|
-
|
|
86
|
-
To use SPA (Single Page Application) navigation you will need to import the `Link` component from `@tanstack/react-router`.
|
|
87
|
-
|
|
88
|
-
```tsx
|
|
89
|
-
import { Link } from '@tanstack/react-router'
|
|
90
|
-
```
|
|
91
|
-
|
|
92
|
-
Then anywhere in your JSX you can use it like so:
|
|
93
|
-
|
|
94
|
-
```tsx
|
|
95
|
-
<Link to="/about">About</Link>
|
|
96
|
-
```
|
|
97
|
-
|
|
98
|
-
This will create a link that will navigate to the `/about` route.
|
|
99
|
-
|
|
100
|
-
More information on the `Link` component can be found in the [Link documentation](https://tanstack.com/router/v1/docs/framework/react/api/router/linkComponent).
|
|
101
|
-
|
|
102
|
-
### Using A Layout
|
|
103
|
-
|
|
104
|
-
Layouts can be used to wrap the contents of the routes in menus, headers, footers, etc.
|
|
105
|
-
|
|
106
|
-
There is already a layout in the `src/main.tsx` file:
|
|
107
|
-
|
|
108
|
-
```tsx
|
|
109
|
-
const rootRoute = createRootRoute({
|
|
110
|
-
component: () => (
|
|
111
|
-
<>
|
|
112
|
-
<Outlet />
|
|
113
|
-
<TanStackRouterDevtools />
|
|
114
|
-
</>
|
|
115
|
-
),
|
|
116
|
-
})
|
|
117
|
-
```
|
|
118
|
-
|
|
119
|
-
You can use the React component specified in the `component` property of the `rootRoute` to wrap the contents of the routes. The `<Outlet />` component is used to render the current route within the body of the layout. For example you could add a header to the layout like so:
|
|
120
|
-
|
|
121
|
-
```tsx
|
|
122
|
-
import { Link } from '@tanstack/react-router'
|
|
123
|
-
|
|
124
|
-
const rootRoute = createRootRoute({
|
|
125
|
-
component: () => (
|
|
126
|
-
<>
|
|
127
|
-
<header>
|
|
128
|
-
<nav>
|
|
129
|
-
<Link to="/">Home</Link>
|
|
130
|
-
<Link to="/about">About</Link>
|
|
131
|
-
</nav>
|
|
132
|
-
</header>
|
|
133
|
-
<Outlet />
|
|
134
|
-
<TanStackRouterDevtools />
|
|
135
|
-
</>
|
|
136
|
-
),
|
|
137
|
-
})
|
|
138
|
-
```
|
|
139
|
-
|
|
140
|
-
The `<TanStackRouterDevtools />` component is not required so you can remove it if you don't want it in your layout.
|
|
141
|
-
|
|
142
|
-
More information on layouts can be found in the [Layouts documentation](https://tanstack.com/router/latest/docs/framework/react/guide/routing-concepts#layouts).
|
|
143
|
-
|
|
144
|
-
### Migrating To File Base Routing
|
|
145
|
-
|
|
146
|
-
First you need to add the Vite plugin for Tanstack Router:
|
|
147
|
-
|
|
148
|
-
```bash
|
|
149
|
-
npm install @tanstack/router-plugin -D
|
|
150
|
-
```
|
|
151
|
-
|
|
152
|
-
From there you need to update your `vite.config.js` file to use the plugin:
|
|
153
|
-
|
|
154
|
-
```ts
|
|
155
|
-
import { defineConfig } from 'vite'
|
|
156
|
-
import viteReact from '@vitejs/plugin-react'
|
|
157
|
-
import { TanStackRouterVite } from '@tanstack/router-plugin/vite'
|
|
158
|
-
import tailwindcss from '@tailwindcss/vite'
|
|
159
|
-
|
|
160
|
-
// https://vitejs.dev/config/
|
|
161
|
-
export default defineConfig({
|
|
162
|
-
plugins: [TanStackRouterVite(), viteReact(), tailwindcss()],
|
|
163
|
-
})
|
|
164
|
-
```
|
|
165
|
-
|
|
166
|
-
Now you'll need to rearrange your files a little bit. That starts with creating a `routes` directory in the `src` directory:
|
|
167
|
-
|
|
168
|
-
```bash
|
|
169
|
-
mkdir src/routes
|
|
170
|
-
```
|
|
171
|
-
|
|
172
|
-
Then you'll need to create a `src/routes/__root.tsx` file with the contents of the root route that was in `main.tsx`.
|
|
173
|
-
|
|
174
|
-
```tsx
|
|
175
|
-
import { Outlet, createRootRoute } from '@tanstack/react-router'
|
|
176
|
-
import { TanStackRouterDevtools } from '@tanstack/react-router-devtools'
|
|
177
|
-
|
|
178
|
-
export const Route = createRootRoute({
|
|
179
|
-
component: () => (
|
|
180
|
-
<>
|
|
181
|
-
<Outlet />
|
|
182
|
-
<TanStackRouterDevtools />
|
|
183
|
-
</>
|
|
184
|
-
),
|
|
185
|
-
})
|
|
186
|
-
```
|
|
187
|
-
|
|
188
|
-
Next up you'll need to move your home route code into `src/routes/index.tsx`
|
|
189
|
-
|
|
190
|
-
```tsx
|
|
191
|
-
import { createFileRoute } from '@tanstack/react-router'
|
|
192
|
-
|
|
193
|
-
import logo from '../logo.svg'
|
|
194
|
-
import '../App.css'
|
|
195
|
-
|
|
196
|
-
export const Route = createFileRoute('/')({
|
|
197
|
-
component: App,
|
|
198
|
-
})
|
|
199
|
-
|
|
200
|
-
function App() {
|
|
201
|
-
return (
|
|
202
|
-
<div className="text-center">
|
|
203
|
-
<header className="min-h-screen flex flex-col items-center justify-center bg-[#282c34] text-white text-[calc(10px+2vmin)]">
|
|
204
|
-
<img
|
|
205
|
-
src={logo}
|
|
206
|
-
className="h-[40vmin] pointer-events-none animate-[spin_20s_linear_infinite]"
|
|
207
|
-
alt="logo"
|
|
208
|
-
/>
|
|
209
|
-
<p>
|
|
210
|
-
Edit <code>src/App.tsx</code> and save to reload.
|
|
211
|
-
</p>
|
|
212
|
-
<a
|
|
213
|
-
className="text-[#61dafb] hover:underline"
|
|
214
|
-
href="https://reactjs.org"
|
|
215
|
-
target="_blank"
|
|
216
|
-
rel="noopener noreferrer"
|
|
217
|
-
>
|
|
218
|
-
Learn React
|
|
219
|
-
</a>
|
|
220
|
-
<a
|
|
221
|
-
className="text-[#61dafb] hover:underline"
|
|
222
|
-
href="https://tanstack.com"
|
|
223
|
-
target="_blank"
|
|
224
|
-
rel="noopener noreferrer"
|
|
225
|
-
>
|
|
226
|
-
Learn TanStack
|
|
227
|
-
</a>
|
|
228
|
-
</header>
|
|
229
|
-
</div>
|
|
230
|
-
)
|
|
231
|
-
}
|
|
232
|
-
```
|
|
233
|
-
|
|
234
|
-
At this point you can delete `src/App.tsx`, you will no longer need it as the contents have moved into `src/routes/index.tsx`.
|
|
235
|
-
|
|
236
|
-
The only additional code is the `createFileRoute` function that tells TanStack Router where to render the route. Helpfully the Vite plugin will keep the path argument that goes to `createFileRoute` automatically in sync with the file system.
|
|
237
|
-
|
|
238
|
-
Finally the `src/main.tsx` file can be simplified down to this:
|
|
239
|
-
|
|
240
|
-
```tsx
|
|
241
|
-
import { StrictMode } from 'react'
|
|
242
|
-
import ReactDOM from 'react-dom/client'
|
|
243
|
-
import { RouterProvider, createRouter } from '@tanstack/react-router'
|
|
244
|
-
|
|
245
|
-
// Import the generated route tree
|
|
246
|
-
import { routeTree } from './routeTree.gen'
|
|
247
|
-
|
|
248
|
-
import './styles.css'
|
|
249
|
-
import reportWebVitals from './reportWebVitals.ts'
|
|
250
|
-
|
|
251
|
-
// Create a new router instance
|
|
252
|
-
const router = createRouter({
|
|
253
|
-
routeTree,
|
|
254
|
-
defaultPreload: 'intent',
|
|
255
|
-
defaultPreloadStaleTime: 0,
|
|
256
|
-
scrollRestoration: true,
|
|
257
|
-
defaultStructuralSharing: true,
|
|
258
|
-
})
|
|
259
|
-
|
|
260
|
-
// Register the router instance for type safety
|
|
261
|
-
declare module '@tanstack/react-router' {
|
|
262
|
-
interface Register {
|
|
263
|
-
router: typeof router
|
|
264
|
-
}
|
|
265
|
-
}
|
|
266
|
-
|
|
267
|
-
// Render the app
|
|
268
|
-
const rootElement = document.getElementById('app')!
|
|
269
|
-
if (!rootElement.innerHTML) {
|
|
270
|
-
const root = ReactDOM.createRoot(rootElement)
|
|
271
|
-
root.render(
|
|
272
|
-
<StrictMode>
|
|
273
|
-
<RouterProvider router={router} />
|
|
274
|
-
</StrictMode>,
|
|
275
|
-
)
|
|
276
|
-
}
|
|
277
|
-
|
|
278
|
-
// If you want to start measuring performance in your app, pass a function
|
|
279
|
-
// to log results (for example: reportWebVitals(console.log))
|
|
280
|
-
// or send to an analytics endpoint. Learn more: https://bit.ly/CRA-vitals
|
|
281
|
-
reportWebVitals()
|
|
282
|
-
```
|
|
283
|
-
|
|
284
|
-
Now you've got a file based routing setup in your project! Let's have some fun with it! Just create a file in `about.tsx` in `src/routes` and it if the application is running TanStack will automatically add contents to the file and you'll have the start of your `/about` route ready to go with no additional work. You can see why folks find File Based Routing so easy to use.
|
|
285
|
-
|
|
286
|
-
You can find out everything you need to know on how to use file based routing in the [File Based Routing](https://tanstack.com/router/latest/docs/framework/react/guide/file-based-routing) documentation.
|
|
287
|
-
|
|
288
|
-
## Data Fetching
|
|
289
|
-
|
|
290
|
-
There are multiple ways to fetch data in your application. You can use TanStack Query to fetch data from a server. But you can also use the `loader` functionality built into TanStack Router to load the data for a route before it's rendered.
|
|
291
|
-
|
|
292
|
-
For example:
|
|
293
|
-
|
|
294
|
-
```tsx
|
|
295
|
-
const peopleRoute = createRoute({
|
|
296
|
-
getParentRoute: () => rootRoute,
|
|
297
|
-
path: '/people',
|
|
298
|
-
loader: async () => {
|
|
299
|
-
const response = await fetch('https://swapi.dev/api/people')
|
|
300
|
-
return response.json() as Promise<{
|
|
301
|
-
results: {
|
|
302
|
-
name: string
|
|
303
|
-
}[]
|
|
304
|
-
}>
|
|
305
|
-
},
|
|
306
|
-
component: () => {
|
|
307
|
-
const data = peopleRoute.useLoaderData()
|
|
308
|
-
return (
|
|
309
|
-
<ul>
|
|
310
|
-
{data.results.map((person) => (
|
|
311
|
-
<li key={person.name}>{person.name}</li>
|
|
312
|
-
))}
|
|
313
|
-
</ul>
|
|
314
|
-
)
|
|
315
|
-
},
|
|
316
|
-
})
|
|
317
|
-
```
|
|
318
|
-
|
|
319
|
-
Loaders simplify your data fetching logic dramatically. Check out more information in the [Loader documentation](https://tanstack.com/router/latest/docs/framework/react/guide/data-loading#loader-parameters).
|
|
320
|
-
|
|
321
|
-
### React-Query
|
|
322
|
-
|
|
323
|
-
React-Query is an excellent addition or alternative to route loading and integrating it into you application is a breeze.
|
|
324
|
-
|
|
325
|
-
First add your dependencies:
|
|
326
|
-
|
|
327
|
-
```bash
|
|
328
|
-
npm install @tanstack/react-query @tanstack/react-query-devtools
|
|
329
|
-
```
|
|
330
|
-
|
|
331
|
-
Next we'll need to create a query client and provider. We recommend putting those in `main.tsx`.
|
|
332
|
-
|
|
333
|
-
```tsx
|
|
334
|
-
import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
|
|
335
|
-
|
|
336
|
-
// ...
|
|
337
|
-
|
|
338
|
-
const queryClient = new QueryClient()
|
|
339
|
-
|
|
340
|
-
// ...
|
|
341
|
-
|
|
342
|
-
if (!rootElement.innerHTML) {
|
|
343
|
-
const root = ReactDOM.createRoot(rootElement)
|
|
344
|
-
|
|
345
|
-
root.render(
|
|
346
|
-
<QueryClientProvider client={queryClient}>
|
|
347
|
-
<RouterProvider router={router} />
|
|
348
|
-
</QueryClientProvider>,
|
|
349
|
-
)
|
|
350
|
-
}
|
|
351
|
-
```
|
|
352
|
-
|
|
353
|
-
You can also add TanStack Query Devtools to the root route (optional).
|
|
354
|
-
|
|
355
|
-
```tsx
|
|
356
|
-
import { ReactQueryDevtools } from '@tanstack/react-query-devtools'
|
|
357
|
-
|
|
358
|
-
const rootRoute = createRootRoute({
|
|
359
|
-
component: () => (
|
|
360
|
-
<>
|
|
361
|
-
<Outlet />
|
|
362
|
-
<ReactQueryDevtools buttonPosition="top-right" />
|
|
363
|
-
<TanStackRouterDevtools />
|
|
364
|
-
</>
|
|
365
|
-
),
|
|
366
|
-
})
|
|
367
|
-
```
|
|
368
|
-
|
|
369
|
-
Now you can use `useQuery` to fetch your data.
|
|
370
|
-
|
|
371
|
-
```tsx
|
|
372
|
-
import { useQuery } from '@tanstack/react-query'
|
|
373
|
-
|
|
374
|
-
import './App.css'
|
|
375
|
-
|
|
376
|
-
function App() {
|
|
377
|
-
const { data } = useQuery({
|
|
378
|
-
queryKey: ['people'],
|
|
379
|
-
queryFn: () =>
|
|
380
|
-
fetch('https://swapi.dev/api/people')
|
|
381
|
-
.then((res) => res.json())
|
|
382
|
-
.then((data) => data.results as { name: string }[]),
|
|
383
|
-
initialData: [],
|
|
384
|
-
})
|
|
385
|
-
|
|
386
|
-
return (
|
|
387
|
-
<div>
|
|
388
|
-
<ul>
|
|
389
|
-
{data.map((person) => (
|
|
390
|
-
<li key={person.name}>{person.name}</li>
|
|
391
|
-
))}
|
|
392
|
-
</ul>
|
|
393
|
-
</div>
|
|
394
|
-
)
|
|
395
|
-
}
|
|
396
|
-
|
|
397
|
-
export default App
|
|
398
|
-
```
|
|
399
|
-
|
|
400
|
-
You can find out everything you need to know on how to use React-Query in the [React-Query documentation](https://tanstack.com/query/latest/docs/framework/react/overview).
|
|
401
|
-
|
|
402
|
-
## State Management
|
|
403
|
-
|
|
404
|
-
Another common requirement for React applications is state management. There are many options for state management in React. TanStack Store provides a great starting point for your project.
|
|
405
|
-
|
|
406
|
-
First you need to add TanStack Store as a dependency:
|
|
407
|
-
|
|
408
|
-
```bash
|
|
409
|
-
npm install @tanstack/store
|
|
410
|
-
```
|
|
411
|
-
|
|
412
|
-
Now let's create a simple counter in the `src/App.tsx` file as a demonstration.
|
|
413
|
-
|
|
414
|
-
```tsx
|
|
415
|
-
import { useStore } from '@tanstack/react-store'
|
|
416
|
-
import { Store } from '@tanstack/store'
|
|
417
|
-
import './App.css'
|
|
418
|
-
|
|
419
|
-
const countStore = new Store(0)
|
|
420
|
-
|
|
421
|
-
function App() {
|
|
422
|
-
const count = useStore(countStore)
|
|
423
|
-
return (
|
|
424
|
-
<div>
|
|
425
|
-
<button onClick={() => countStore.setState((n) => n + 1)}>
|
|
426
|
-
Increment - {count}
|
|
427
|
-
</button>
|
|
428
|
-
</div>
|
|
429
|
-
)
|
|
430
|
-
}
|
|
431
|
-
|
|
432
|
-
export default App
|
|
433
|
-
```
|
|
434
|
-
|
|
435
|
-
One of the many nice features of TanStack Store is the ability to derive state from other state. That derived state will update when the base state updates.
|
|
436
|
-
|
|
437
|
-
Let's check this out by doubling the count using derived state.
|
|
438
|
-
|
|
439
|
-
```tsx
|
|
440
|
-
import { useStore } from '@tanstack/react-store'
|
|
441
|
-
import { Store, Derived } from '@tanstack/store'
|
|
442
|
-
import './App.css'
|
|
443
|
-
|
|
444
|
-
const countStore = new Store(0)
|
|
445
|
-
|
|
446
|
-
const doubledStore = new Derived({
|
|
447
|
-
fn: () => countStore.state * 2,
|
|
448
|
-
deps: [countStore],
|
|
449
|
-
})
|
|
450
|
-
doubledStore.mount()
|
|
451
|
-
|
|
452
|
-
function App() {
|
|
453
|
-
const count = useStore(countStore)
|
|
454
|
-
const doubledCount = useStore(doubledStore)
|
|
455
|
-
|
|
456
|
-
return (
|
|
457
|
-
<div>
|
|
458
|
-
<button onClick={() => countStore.setState((n) => n + 1)}>
|
|
459
|
-
Increment - {count}
|
|
460
|
-
</button>
|
|
461
|
-
<div>Doubled - {doubledCount}</div>
|
|
462
|
-
</div>
|
|
463
|
-
)
|
|
464
|
-
}
|
|
465
|
-
|
|
466
|
-
export default App
|
|
467
|
-
```
|
|
468
|
-
|
|
469
|
-
We use the `Derived` class to create a new store that is derived from another store. The `Derived` class has a `mount` method that will start the derived store updating.
|
|
470
|
-
|
|
471
|
-
Once we've created the derived store we can use it in the `App` component just like we would any other store using the `useStore` hook.
|
|
472
|
-
|
|
473
|
-
You can find out everything you need to know on how to use TanStack Store in the [TanStack Store documentation](https://tanstack.com/store/latest).
|
|
474
|
-
|
|
475
|
-
# Demo files
|
|
476
|
-
|
|
477
|
-
Files prefixed with `demo` can be safely deleted. They are there to provide a starting point for you to play around with the features you've installed.
|
|
478
|
-
|
|
479
|
-
# Learn More
|
|
480
|
-
|
|
481
|
-
You can learn more about all of the offerings from TanStack in the [TanStack documentation](https://tanstack.com).
|
|
1
|
+
Welcome to your new TanStack app!
|
|
2
|
+
|
|
3
|
+
# Getting Started
|
|
4
|
+
|
|
5
|
+
To run this application:
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install
|
|
9
|
+
npm run start
|
|
10
|
+
```
|
|
11
|
+
|
|
12
|
+
# Building For Production
|
|
13
|
+
|
|
14
|
+
To build this application for production:
|
|
15
|
+
|
|
16
|
+
```bash
|
|
17
|
+
npm run build
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
## Testing
|
|
21
|
+
|
|
22
|
+
This project uses [Vitest](https://vitest.dev/) for testing. You can run the tests with:
|
|
23
|
+
|
|
24
|
+
```bash
|
|
25
|
+
npm run test
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
## Styling
|
|
29
|
+
|
|
30
|
+
This project uses [Tailwind CSS](https://tailwindcss.com/) for styling.
|
|
31
|
+
|
|
32
|
+
## Linting & Formatting
|
|
33
|
+
|
|
34
|
+
This project uses [eslint](https://eslint.org/) and [prettier](https://prettier.io/) for linting and formatting. Eslint is configured using [tanstack/eslint-config](https://tanstack.com/config/latest/docs/eslint). The following scripts are available:
|
|
35
|
+
|
|
36
|
+
```bash
|
|
37
|
+
npm run lint
|
|
38
|
+
npm run format
|
|
39
|
+
npm run check
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
## Routing
|
|
43
|
+
|
|
44
|
+
This project uses [TanStack Router](https://tanstack.com/router). The initial setup is a code based router. Which means that the routes are defined in code (in the `./src/main.tsx` file). If you like you can also use a file based routing setup by following the [File Based Routing](https://tanstack.com/router/latest/docs/framework/react/guide/file-based-routing) guide.
|
|
45
|
+
|
|
46
|
+
### Adding A Route
|
|
47
|
+
|
|
48
|
+
To add a new route to your application just add another `createRoute` call to the `./src/main.tsx` file. The example below adds a new `/about`route to the root route.
|
|
49
|
+
|
|
50
|
+
```tsx
|
|
51
|
+
const aboutRoute = createRoute({
|
|
52
|
+
getParentRoute: () => rootRoute,
|
|
53
|
+
path: '/about',
|
|
54
|
+
component: () => <h1>About</h1>,
|
|
55
|
+
})
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
You will also need to add the route to the `routeTree` in the `./src/main.tsx` file.
|
|
59
|
+
|
|
60
|
+
```tsx
|
|
61
|
+
const routeTree = rootRoute.addChildren([indexRoute, aboutRoute])
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
With this set up you should be able to navigate to `/about` and see the about page.
|
|
65
|
+
|
|
66
|
+
Of course you don't need to implement the About page in the `main.tsx` file. You can create that component in another file and import it into the `main.tsx` file, then use it in the `component` property of the `createRoute` call, like so:
|
|
67
|
+
|
|
68
|
+
```tsx
|
|
69
|
+
import About from './components/About.tsx'
|
|
70
|
+
|
|
71
|
+
const aboutRoute = createRoute({
|
|
72
|
+
getParentRoute: () => rootRoute,
|
|
73
|
+
path: '/about',
|
|
74
|
+
component: About,
|
|
75
|
+
})
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
That is how we have the `App` component set up with the home page.
|
|
79
|
+
|
|
80
|
+
For more information on the options you have when you are creating code based routes check out the [Code Based Routing](https://tanstack.com/router/latest/docs/framework/react/guide/code-based-routing) documentation.
|
|
81
|
+
|
|
82
|
+
Now that you have two routes you can use a `Link` component to navigate between them.
|
|
83
|
+
|
|
84
|
+
### Adding Links
|
|
85
|
+
|
|
86
|
+
To use SPA (Single Page Application) navigation you will need to import the `Link` component from `@tanstack/react-router`.
|
|
87
|
+
|
|
88
|
+
```tsx
|
|
89
|
+
import { Link } from '@tanstack/react-router'
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
Then anywhere in your JSX you can use it like so:
|
|
93
|
+
|
|
94
|
+
```tsx
|
|
95
|
+
<Link to="/about">About</Link>
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
This will create a link that will navigate to the `/about` route.
|
|
99
|
+
|
|
100
|
+
More information on the `Link` component can be found in the [Link documentation](https://tanstack.com/router/v1/docs/framework/react/api/router/linkComponent).
|
|
101
|
+
|
|
102
|
+
### Using A Layout
|
|
103
|
+
|
|
104
|
+
Layouts can be used to wrap the contents of the routes in menus, headers, footers, etc.
|
|
105
|
+
|
|
106
|
+
There is already a layout in the `src/main.tsx` file:
|
|
107
|
+
|
|
108
|
+
```tsx
|
|
109
|
+
const rootRoute = createRootRoute({
|
|
110
|
+
component: () => (
|
|
111
|
+
<>
|
|
112
|
+
<Outlet />
|
|
113
|
+
<TanStackRouterDevtools />
|
|
114
|
+
</>
|
|
115
|
+
),
|
|
116
|
+
})
|
|
117
|
+
```
|
|
118
|
+
|
|
119
|
+
You can use the React component specified in the `component` property of the `rootRoute` to wrap the contents of the routes. The `<Outlet />` component is used to render the current route within the body of the layout. For example you could add a header to the layout like so:
|
|
120
|
+
|
|
121
|
+
```tsx
|
|
122
|
+
import { Link } from '@tanstack/react-router'
|
|
123
|
+
|
|
124
|
+
const rootRoute = createRootRoute({
|
|
125
|
+
component: () => (
|
|
126
|
+
<>
|
|
127
|
+
<header>
|
|
128
|
+
<nav>
|
|
129
|
+
<Link to="/">Home</Link>
|
|
130
|
+
<Link to="/about">About</Link>
|
|
131
|
+
</nav>
|
|
132
|
+
</header>
|
|
133
|
+
<Outlet />
|
|
134
|
+
<TanStackRouterDevtools />
|
|
135
|
+
</>
|
|
136
|
+
),
|
|
137
|
+
})
|
|
138
|
+
```
|
|
139
|
+
|
|
140
|
+
The `<TanStackRouterDevtools />` component is not required so you can remove it if you don't want it in your layout.
|
|
141
|
+
|
|
142
|
+
More information on layouts can be found in the [Layouts documentation](https://tanstack.com/router/latest/docs/framework/react/guide/routing-concepts#layouts).
|
|
143
|
+
|
|
144
|
+
### Migrating To File Base Routing
|
|
145
|
+
|
|
146
|
+
First you need to add the Vite plugin for Tanstack Router:
|
|
147
|
+
|
|
148
|
+
```bash
|
|
149
|
+
npm install @tanstack/router-plugin -D
|
|
150
|
+
```
|
|
151
|
+
|
|
152
|
+
From there you need to update your `vite.config.js` file to use the plugin:
|
|
153
|
+
|
|
154
|
+
```ts
|
|
155
|
+
import { defineConfig } from 'vite'
|
|
156
|
+
import viteReact from '@vitejs/plugin-react'
|
|
157
|
+
import { TanStackRouterVite } from '@tanstack/router-plugin/vite'
|
|
158
|
+
import tailwindcss from '@tailwindcss/vite'
|
|
159
|
+
|
|
160
|
+
// https://vitejs.dev/config/
|
|
161
|
+
export default defineConfig({
|
|
162
|
+
plugins: [TanStackRouterVite(), viteReact(), tailwindcss()],
|
|
163
|
+
})
|
|
164
|
+
```
|
|
165
|
+
|
|
166
|
+
Now you'll need to rearrange your files a little bit. That starts with creating a `routes` directory in the `src` directory:
|
|
167
|
+
|
|
168
|
+
```bash
|
|
169
|
+
mkdir src/routes
|
|
170
|
+
```
|
|
171
|
+
|
|
172
|
+
Then you'll need to create a `src/routes/__root.tsx` file with the contents of the root route that was in `main.tsx`.
|
|
173
|
+
|
|
174
|
+
```tsx
|
|
175
|
+
import { Outlet, createRootRoute } from '@tanstack/react-router'
|
|
176
|
+
import { TanStackRouterDevtools } from '@tanstack/react-router-devtools'
|
|
177
|
+
|
|
178
|
+
export const Route = createRootRoute({
|
|
179
|
+
component: () => (
|
|
180
|
+
<>
|
|
181
|
+
<Outlet />
|
|
182
|
+
<TanStackRouterDevtools />
|
|
183
|
+
</>
|
|
184
|
+
),
|
|
185
|
+
})
|
|
186
|
+
```
|
|
187
|
+
|
|
188
|
+
Next up you'll need to move your home route code into `src/routes/index.tsx`
|
|
189
|
+
|
|
190
|
+
```tsx
|
|
191
|
+
import { createFileRoute } from '@tanstack/react-router'
|
|
192
|
+
|
|
193
|
+
import logo from '../logo.svg'
|
|
194
|
+
import '../App.css'
|
|
195
|
+
|
|
196
|
+
export const Route = createFileRoute('/')({
|
|
197
|
+
component: App,
|
|
198
|
+
})
|
|
199
|
+
|
|
200
|
+
function App() {
|
|
201
|
+
return (
|
|
202
|
+
<div className="text-center">
|
|
203
|
+
<header className="min-h-screen flex flex-col items-center justify-center bg-[#282c34] text-white text-[calc(10px+2vmin)]">
|
|
204
|
+
<img
|
|
205
|
+
src={logo}
|
|
206
|
+
className="h-[40vmin] pointer-events-none animate-[spin_20s_linear_infinite]"
|
|
207
|
+
alt="logo"
|
|
208
|
+
/>
|
|
209
|
+
<p>
|
|
210
|
+
Edit <code>src/App.tsx</code> and save to reload.
|
|
211
|
+
</p>
|
|
212
|
+
<a
|
|
213
|
+
className="text-[#61dafb] hover:underline"
|
|
214
|
+
href="https://reactjs.org"
|
|
215
|
+
target="_blank"
|
|
216
|
+
rel="noopener noreferrer"
|
|
217
|
+
>
|
|
218
|
+
Learn React
|
|
219
|
+
</a>
|
|
220
|
+
<a
|
|
221
|
+
className="text-[#61dafb] hover:underline"
|
|
222
|
+
href="https://tanstack.com"
|
|
223
|
+
target="_blank"
|
|
224
|
+
rel="noopener noreferrer"
|
|
225
|
+
>
|
|
226
|
+
Learn TanStack
|
|
227
|
+
</a>
|
|
228
|
+
</header>
|
|
229
|
+
</div>
|
|
230
|
+
)
|
|
231
|
+
}
|
|
232
|
+
```
|
|
233
|
+
|
|
234
|
+
At this point you can delete `src/App.tsx`, you will no longer need it as the contents have moved into `src/routes/index.tsx`.
|
|
235
|
+
|
|
236
|
+
The only additional code is the `createFileRoute` function that tells TanStack Router where to render the route. Helpfully the Vite plugin will keep the path argument that goes to `createFileRoute` automatically in sync with the file system.
|
|
237
|
+
|
|
238
|
+
Finally the `src/main.tsx` file can be simplified down to this:
|
|
239
|
+
|
|
240
|
+
```tsx
|
|
241
|
+
import { StrictMode } from 'react'
|
|
242
|
+
import ReactDOM from 'react-dom/client'
|
|
243
|
+
import { RouterProvider, createRouter } from '@tanstack/react-router'
|
|
244
|
+
|
|
245
|
+
// Import the generated route tree
|
|
246
|
+
import { routeTree } from './routeTree.gen'
|
|
247
|
+
|
|
248
|
+
import './styles.css'
|
|
249
|
+
import reportWebVitals from './reportWebVitals.ts'
|
|
250
|
+
|
|
251
|
+
// Create a new router instance
|
|
252
|
+
const router = createRouter({
|
|
253
|
+
routeTree,
|
|
254
|
+
defaultPreload: 'intent',
|
|
255
|
+
defaultPreloadStaleTime: 0,
|
|
256
|
+
scrollRestoration: true,
|
|
257
|
+
defaultStructuralSharing: true,
|
|
258
|
+
})
|
|
259
|
+
|
|
260
|
+
// Register the router instance for type safety
|
|
261
|
+
declare module '@tanstack/react-router' {
|
|
262
|
+
interface Register {
|
|
263
|
+
router: typeof router
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
// Render the app
|
|
268
|
+
const rootElement = document.getElementById('app')!
|
|
269
|
+
if (!rootElement.innerHTML) {
|
|
270
|
+
const root = ReactDOM.createRoot(rootElement)
|
|
271
|
+
root.render(
|
|
272
|
+
<StrictMode>
|
|
273
|
+
<RouterProvider router={router} />
|
|
274
|
+
</StrictMode>,
|
|
275
|
+
)
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
// If you want to start measuring performance in your app, pass a function
|
|
279
|
+
// to log results (for example: reportWebVitals(console.log))
|
|
280
|
+
// or send to an analytics endpoint. Learn more: https://bit.ly/CRA-vitals
|
|
281
|
+
reportWebVitals()
|
|
282
|
+
```
|
|
283
|
+
|
|
284
|
+
Now you've got a file based routing setup in your project! Let's have some fun with it! Just create a file in `about.tsx` in `src/routes` and it if the application is running TanStack will automatically add contents to the file and you'll have the start of your `/about` route ready to go with no additional work. You can see why folks find File Based Routing so easy to use.
|
|
285
|
+
|
|
286
|
+
You can find out everything you need to know on how to use file based routing in the [File Based Routing](https://tanstack.com/router/latest/docs/framework/react/guide/file-based-routing) documentation.
|
|
287
|
+
|
|
288
|
+
## Data Fetching
|
|
289
|
+
|
|
290
|
+
There are multiple ways to fetch data in your application. You can use TanStack Query to fetch data from a server. But you can also use the `loader` functionality built into TanStack Router to load the data for a route before it's rendered.
|
|
291
|
+
|
|
292
|
+
For example:
|
|
293
|
+
|
|
294
|
+
```tsx
|
|
295
|
+
const peopleRoute = createRoute({
|
|
296
|
+
getParentRoute: () => rootRoute,
|
|
297
|
+
path: '/people',
|
|
298
|
+
loader: async () => {
|
|
299
|
+
const response = await fetch('https://swapi.dev/api/people')
|
|
300
|
+
return response.json() as Promise<{
|
|
301
|
+
results: {
|
|
302
|
+
name: string
|
|
303
|
+
}[]
|
|
304
|
+
}>
|
|
305
|
+
},
|
|
306
|
+
component: () => {
|
|
307
|
+
const data = peopleRoute.useLoaderData()
|
|
308
|
+
return (
|
|
309
|
+
<ul>
|
|
310
|
+
{data.results.map((person) => (
|
|
311
|
+
<li key={person.name}>{person.name}</li>
|
|
312
|
+
))}
|
|
313
|
+
</ul>
|
|
314
|
+
)
|
|
315
|
+
},
|
|
316
|
+
})
|
|
317
|
+
```
|
|
318
|
+
|
|
319
|
+
Loaders simplify your data fetching logic dramatically. Check out more information in the [Loader documentation](https://tanstack.com/router/latest/docs/framework/react/guide/data-loading#loader-parameters).
|
|
320
|
+
|
|
321
|
+
### React-Query
|
|
322
|
+
|
|
323
|
+
React-Query is an excellent addition or alternative to route loading and integrating it into you application is a breeze.
|
|
324
|
+
|
|
325
|
+
First add your dependencies:
|
|
326
|
+
|
|
327
|
+
```bash
|
|
328
|
+
npm install @tanstack/react-query @tanstack/react-query-devtools
|
|
329
|
+
```
|
|
330
|
+
|
|
331
|
+
Next we'll need to create a query client and provider. We recommend putting those in `main.tsx`.
|
|
332
|
+
|
|
333
|
+
```tsx
|
|
334
|
+
import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
|
|
335
|
+
|
|
336
|
+
// ...
|
|
337
|
+
|
|
338
|
+
const queryClient = new QueryClient()
|
|
339
|
+
|
|
340
|
+
// ...
|
|
341
|
+
|
|
342
|
+
if (!rootElement.innerHTML) {
|
|
343
|
+
const root = ReactDOM.createRoot(rootElement)
|
|
344
|
+
|
|
345
|
+
root.render(
|
|
346
|
+
<QueryClientProvider client={queryClient}>
|
|
347
|
+
<RouterProvider router={router} />
|
|
348
|
+
</QueryClientProvider>,
|
|
349
|
+
)
|
|
350
|
+
}
|
|
351
|
+
```
|
|
352
|
+
|
|
353
|
+
You can also add TanStack Query Devtools to the root route (optional).
|
|
354
|
+
|
|
355
|
+
```tsx
|
|
356
|
+
import { ReactQueryDevtools } from '@tanstack/react-query-devtools'
|
|
357
|
+
|
|
358
|
+
const rootRoute = createRootRoute({
|
|
359
|
+
component: () => (
|
|
360
|
+
<>
|
|
361
|
+
<Outlet />
|
|
362
|
+
<ReactQueryDevtools buttonPosition="top-right" />
|
|
363
|
+
<TanStackRouterDevtools />
|
|
364
|
+
</>
|
|
365
|
+
),
|
|
366
|
+
})
|
|
367
|
+
```
|
|
368
|
+
|
|
369
|
+
Now you can use `useQuery` to fetch your data.
|
|
370
|
+
|
|
371
|
+
```tsx
|
|
372
|
+
import { useQuery } from '@tanstack/react-query'
|
|
373
|
+
|
|
374
|
+
import './App.css'
|
|
375
|
+
|
|
376
|
+
function App() {
|
|
377
|
+
const { data } = useQuery({
|
|
378
|
+
queryKey: ['people'],
|
|
379
|
+
queryFn: () =>
|
|
380
|
+
fetch('https://swapi.dev/api/people')
|
|
381
|
+
.then((res) => res.json())
|
|
382
|
+
.then((data) => data.results as { name: string }[]),
|
|
383
|
+
initialData: [],
|
|
384
|
+
})
|
|
385
|
+
|
|
386
|
+
return (
|
|
387
|
+
<div>
|
|
388
|
+
<ul>
|
|
389
|
+
{data.map((person) => (
|
|
390
|
+
<li key={person.name}>{person.name}</li>
|
|
391
|
+
))}
|
|
392
|
+
</ul>
|
|
393
|
+
</div>
|
|
394
|
+
)
|
|
395
|
+
}
|
|
396
|
+
|
|
397
|
+
export default App
|
|
398
|
+
```
|
|
399
|
+
|
|
400
|
+
You can find out everything you need to know on how to use React-Query in the [React-Query documentation](https://tanstack.com/query/latest/docs/framework/react/overview).
|
|
401
|
+
|
|
402
|
+
## State Management
|
|
403
|
+
|
|
404
|
+
Another common requirement for React applications is state management. There are many options for state management in React. TanStack Store provides a great starting point for your project.
|
|
405
|
+
|
|
406
|
+
First you need to add TanStack Store as a dependency:
|
|
407
|
+
|
|
408
|
+
```bash
|
|
409
|
+
npm install @tanstack/store
|
|
410
|
+
```
|
|
411
|
+
|
|
412
|
+
Now let's create a simple counter in the `src/App.tsx` file as a demonstration.
|
|
413
|
+
|
|
414
|
+
```tsx
|
|
415
|
+
import { useStore } from '@tanstack/react-store'
|
|
416
|
+
import { Store } from '@tanstack/store'
|
|
417
|
+
import './App.css'
|
|
418
|
+
|
|
419
|
+
const countStore = new Store(0)
|
|
420
|
+
|
|
421
|
+
function App() {
|
|
422
|
+
const count = useStore(countStore)
|
|
423
|
+
return (
|
|
424
|
+
<div>
|
|
425
|
+
<button onClick={() => countStore.setState((n) => n + 1)}>
|
|
426
|
+
Increment - {count}
|
|
427
|
+
</button>
|
|
428
|
+
</div>
|
|
429
|
+
)
|
|
430
|
+
}
|
|
431
|
+
|
|
432
|
+
export default App
|
|
433
|
+
```
|
|
434
|
+
|
|
435
|
+
One of the many nice features of TanStack Store is the ability to derive state from other state. That derived state will update when the base state updates.
|
|
436
|
+
|
|
437
|
+
Let's check this out by doubling the count using derived state.
|
|
438
|
+
|
|
439
|
+
```tsx
|
|
440
|
+
import { useStore } from '@tanstack/react-store'
|
|
441
|
+
import { Store, Derived } from '@tanstack/store'
|
|
442
|
+
import './App.css'
|
|
443
|
+
|
|
444
|
+
const countStore = new Store(0)
|
|
445
|
+
|
|
446
|
+
const doubledStore = new Derived({
|
|
447
|
+
fn: () => countStore.state * 2,
|
|
448
|
+
deps: [countStore],
|
|
449
|
+
})
|
|
450
|
+
doubledStore.mount()
|
|
451
|
+
|
|
452
|
+
function App() {
|
|
453
|
+
const count = useStore(countStore)
|
|
454
|
+
const doubledCount = useStore(doubledStore)
|
|
455
|
+
|
|
456
|
+
return (
|
|
457
|
+
<div>
|
|
458
|
+
<button onClick={() => countStore.setState((n) => n + 1)}>
|
|
459
|
+
Increment - {count}
|
|
460
|
+
</button>
|
|
461
|
+
<div>Doubled - {doubledCount}</div>
|
|
462
|
+
</div>
|
|
463
|
+
)
|
|
464
|
+
}
|
|
465
|
+
|
|
466
|
+
export default App
|
|
467
|
+
```
|
|
468
|
+
|
|
469
|
+
We use the `Derived` class to create a new store that is derived from another store. The `Derived` class has a `mount` method that will start the derived store updating.
|
|
470
|
+
|
|
471
|
+
Once we've created the derived store we can use it in the `App` component just like we would any other store using the `useStore` hook.
|
|
472
|
+
|
|
473
|
+
You can find out everything you need to know on how to use TanStack Store in the [TanStack Store documentation](https://tanstack.com/store/latest).
|
|
474
|
+
|
|
475
|
+
# Demo files
|
|
476
|
+
|
|
477
|
+
Files prefixed with `demo` can be safely deleted. They are there to provide a starting point for you to play around with the features you've installed.
|
|
478
|
+
|
|
479
|
+
# Learn More
|
|
480
|
+
|
|
481
|
+
You can learn more about all of the offerings from TanStack in the [TanStack documentation](https://tanstack.com).
|