docs-i18n 0.8.1 → 0.8.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/admin/dist/server/server.js +20 -20
- package/package.json +1 -1
- package/template/app/utils/content-loader.ts +4 -0
- package/template/app/utils/docs.server.ts +7 -0
- package/template/content/blog/en/announcing-query-v5.md +110 -0
- package/template/content/blog/en/hello-world.md +26 -0
- package/template/content/blog/en/i18n-best-practices.md +57 -0
- package/template/content/blog/en/react-query-vs-swr.md +100 -0
- package/template/content/blog/en/state-management-2024.md +143 -0
- package/template/content/blog/en/tanstack-router-1.0.md +121 -0
- package/template/content/blog/ja/announcing-query-v5.md +110 -0
- package/template/content/blog/ja/hello-world.md +26 -0
- package/template/content/blog/zh-hans/announcing-query-v5.md +93 -0
- package/template/content/blog/zh-hans/hello-world.md +26 -0
- package/template/content/docs-i18n/docs.config.json +25 -0
- package/template/content/docs-i18n/en/architecture.md +222 -0
- package/template/content/docs-i18n/en/configuration.md +331 -0
- package/template/content/docs-i18n/en/deployment.md +209 -0
- package/template/content/docs-i18n/en/getting-started.md +168 -0
- package/template/content/docs.config.json +25 -0
- package/template/content/en/admin.md +151 -0
- package/template/content/en/architecture.md +222 -0
- package/template/content/en/cli.md +269 -0
- package/template/content/en/configuration.md +331 -0
- package/template/content/en/deployment.md +209 -0
- package/template/content/en/getting-started.md +168 -0
- package/template/content/form/docs.config.json +18 -0
- package/template/content/form/en/guides/validation.md +175 -0
- package/template/content/form/en/installation.md +63 -0
- package/template/content/form/en/overview.md +71 -0
- package/template/content/form/en/quick-start.md +121 -0
- package/template/content/form/ja/installation.md +63 -0
- package/template/content/form/ja/overview.md +71 -0
- package/template/content/form/zh-hans/installation.md +63 -0
- package/template/content/form/zh-hans/overview.md +71 -0
- package/template/content/query/docs.config.json +32 -0
- package/template/content/query/en/guides/mutations.md +126 -0
- package/template/content/query/en/guides/pagination.md +98 -0
- package/template/content/query/en/guides/queries.md +120 -0
- package/template/content/query/en/installation.md +78 -0
- package/template/content/query/en/overview.md +72 -0
- package/template/content/query/en/quick-start.md +108 -0
- package/template/content/query/ja/installation.md +78 -0
- package/template/content/query/ja/overview.md +72 -0
- package/template/content/query/zh-hans/guides/mutations.md +126 -0
- package/template/content/query/zh-hans/guides/pagination.md +98 -0
- package/template/content/query/zh-hans/guides/queries.md +120 -0
- package/template/content/query/zh-hans/installation.md +95 -0
- package/template/content/query/zh-hans/overview.md +72 -0
- package/template/content/query/zh-hans/quick-start.md +108 -0
- package/template/content/router/docs.config.json +18 -0
- package/template/content/router/en/guides/routing-concepts.md +131 -0
- package/template/content/router/en/installation.md +57 -0
- package/template/content/router/en/overview.md +74 -0
- package/template/content/router/en/quick-start.md +88 -0
- package/template/content/router/ja/installation.md +57 -0
- package/template/content/router/ja/overview.md +78 -0
- package/template/content/router/zh-hans/guides/routing-concepts.md +131 -0
- package/template/content/router/zh-hans/installation.md +57 -0
- package/template/content/router/zh-hans/overview.md +81 -0
- package/template/content/router/zh-hans/quick-start.md +88 -0
- package/template/content/table/docs.config.json +18 -0
- package/template/content/table/en/guides/column-definitions.md +135 -0
- package/template/content/table/en/installation.md +56 -0
- package/template/content/table/en/overview.md +79 -0
- package/template/content/table/en/quick-start.md +112 -0
- package/template/content/table/ja/installation.md +56 -0
- package/template/content/table/ja/overview.md +79 -0
- package/template/content/table/zh-hans/installation.md +56 -0
- package/template/content/table/zh-hans/overview.md +79 -0
- package/template/content/virtual/docs.config.json +18 -0
- package/template/content/virtual/en/guides/dynamic-sizing.md +129 -0
- package/template/content/virtual/en/installation.md +57 -0
- package/template/content/virtual/en/overview.md +74 -0
- package/template/content/virtual/en/quick-start.md +114 -0
- package/template/content/virtual/ja/installation.md +57 -0
- package/template/content/virtual/ja/overview.md +74 -0
- package/template/content/virtual/zh-hans/installation.md +57 -0
- package/template/content/virtual/zh-hans/overview.md +74 -0
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: 快速开始
|
|
3
|
+
description: 几分钟内上手 TanStack Query
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
# 快速开始
|
|
7
|
+
|
|
8
|
+
本指南将帮助您尽快上手 TanStack Query。
|
|
9
|
+
|
|
10
|
+
## 设置 Provider
|
|
11
|
+
|
|
12
|
+
使用 `QueryClientProvider` 包裹您的应用,并传入一个 `QueryClient` 实例:
|
|
13
|
+
|
|
14
|
+
```tsx
|
|
15
|
+
import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
|
|
16
|
+
|
|
17
|
+
const queryClient = new QueryClient()
|
|
18
|
+
|
|
19
|
+
function App() {
|
|
20
|
+
return (
|
|
21
|
+
<QueryClientProvider client={queryClient}>
|
|
22
|
+
<MyApp />
|
|
23
|
+
</QueryClientProvider>
|
|
24
|
+
)
|
|
25
|
+
}
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
## 您的第一个查询
|
|
29
|
+
|
|
30
|
+
使用 `useQuery` Hook 来获取数据:
|
|
31
|
+
|
|
32
|
+
```tsx
|
|
33
|
+
import { useQuery } from '@tanstack/react-query'
|
|
34
|
+
|
|
35
|
+
function Todos() {
|
|
36
|
+
const { data, isLoading, error } = useQuery({
|
|
37
|
+
queryKey: ['todos'],
|
|
38
|
+
queryFn: fetchTodos,
|
|
39
|
+
})
|
|
40
|
+
|
|
41
|
+
if (isLoading) return <span>加载中...</span>
|
|
42
|
+
if (error) return <span>错误:{error.message}</span>
|
|
43
|
+
|
|
44
|
+
return (
|
|
45
|
+
<ul>
|
|
46
|
+
{data.map((todo) => (
|
|
47
|
+
<li key={todo.id}>{todo.title}</li>
|
|
48
|
+
))}
|
|
49
|
+
</ul>
|
|
50
|
+
)
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
async function fetchTodos() {
|
|
54
|
+
const res = await fetch('https://jsonplaceholder.typicode.com/todos')
|
|
55
|
+
if (!res.ok) throw new Error('Failed to fetch')
|
|
56
|
+
return res.json()
|
|
57
|
+
}
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
## 您的第一个变更操作
|
|
61
|
+
|
|
62
|
+
使用 `useMutation` 来创建、更新或删除数据:
|
|
63
|
+
|
|
64
|
+
```tsx
|
|
65
|
+
import { useMutation, useQueryClient } from '@tanstack/react-query'
|
|
66
|
+
|
|
67
|
+
function AddTodo() {
|
|
68
|
+
const queryClient = useQueryClient()
|
|
69
|
+
|
|
70
|
+
const mutation = useMutation({
|
|
71
|
+
mutationFn: (newTodo: { title: string }) =>
|
|
72
|
+
fetch('/api/todos', {
|
|
73
|
+
method: 'POST',
|
|
74
|
+
body: JSON.stringify(newTodo),
|
|
75
|
+
}).then((res) => res.json()),
|
|
76
|
+
onSuccess: () => {
|
|
77
|
+
// 使缓存失效并重新获取
|
|
78
|
+
queryClient.invalidateQueries({ queryKey: ['todos'] })
|
|
79
|
+
},
|
|
80
|
+
})
|
|
81
|
+
|
|
82
|
+
return (
|
|
83
|
+
<button onClick={() => mutation.mutate({ title: 'New Todo' })}>
|
|
84
|
+
添加待办事项
|
|
85
|
+
</button>
|
|
86
|
+
)
|
|
87
|
+
}
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
## 添加开发工具
|
|
91
|
+
|
|
92
|
+
添加 React Query Devtools 进行调试:
|
|
93
|
+
|
|
94
|
+
```tsx
|
|
95
|
+
import { ReactQueryDevtools } from '@tanstack/react-query-devtools'
|
|
96
|
+
|
|
97
|
+
function App() {
|
|
98
|
+
return (
|
|
99
|
+
<QueryClientProvider client={queryClient}>
|
|
100
|
+
<MyApp />
|
|
101
|
+
<ReactQueryDevtools initialIsOpen={false} />
|
|
102
|
+
</QueryClientProvider>
|
|
103
|
+
)
|
|
104
|
+
}
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
> [!NOTE]
|
|
108
|
+
> 开发工具在生产构建中会自动排除。
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
{
|
|
2
|
+
"sections": [
|
|
3
|
+
{
|
|
4
|
+
"label": "Getting Started",
|
|
5
|
+
"children": [
|
|
6
|
+
{ "label": "Overview", "to": "overview" },
|
|
7
|
+
{ "label": "Installation", "to": "installation" },
|
|
8
|
+
{ "label": "Quick Start", "to": "quick-start" }
|
|
9
|
+
]
|
|
10
|
+
},
|
|
11
|
+
{
|
|
12
|
+
"label": "Guides",
|
|
13
|
+
"children": [
|
|
14
|
+
{ "label": "Routing Concepts", "to": "guides/routing-concepts" }
|
|
15
|
+
]
|
|
16
|
+
}
|
|
17
|
+
]
|
|
18
|
+
}
|
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: Routing Concepts
|
|
3
|
+
description: Understand the core routing concepts in TanStack Router
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
# Routing Concepts
|
|
7
|
+
|
|
8
|
+
TanStack Router is built on a few core concepts that make it powerful and flexible. Understanding these concepts will help you build better applications.
|
|
9
|
+
|
|
10
|
+
## Route Trees
|
|
11
|
+
|
|
12
|
+
TanStack Router uses a route tree to define the structure of your application. Routes can be nested to create layouts and hierarchies:
|
|
13
|
+
|
|
14
|
+
```tsx
|
|
15
|
+
import { createRootRoute, createRoute, createRouter } from '@tanstack/react-router'
|
|
16
|
+
|
|
17
|
+
const rootRoute = createRootRoute({
|
|
18
|
+
component: RootLayout,
|
|
19
|
+
})
|
|
20
|
+
|
|
21
|
+
const indexRoute = createRoute({
|
|
22
|
+
getParentRoute: () => rootRoute,
|
|
23
|
+
path: '/',
|
|
24
|
+
component: HomePage,
|
|
25
|
+
})
|
|
26
|
+
|
|
27
|
+
const aboutRoute = createRoute({
|
|
28
|
+
getParentRoute: () => rootRoute,
|
|
29
|
+
path: '/about',
|
|
30
|
+
component: AboutPage,
|
|
31
|
+
})
|
|
32
|
+
|
|
33
|
+
const routeTree = rootRoute.addChildren([indexRoute, aboutRoute])
|
|
34
|
+
|
|
35
|
+
const router = createRouter({ routeTree })
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
## File-Based Routing
|
|
39
|
+
|
|
40
|
+
For most applications, TanStack Router supports file-based routing through a plugin. Your file structure becomes your route structure:
|
|
41
|
+
|
|
42
|
+
```
|
|
43
|
+
app/
|
|
44
|
+
routes/
|
|
45
|
+
__root.tsx → Root layout
|
|
46
|
+
index.tsx → /
|
|
47
|
+
about.tsx → /about
|
|
48
|
+
posts.tsx → /posts (layout)
|
|
49
|
+
posts.index.tsx → /posts/
|
|
50
|
+
posts.$postId.tsx → /posts/:postId
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
> [!NOTE]
|
|
54
|
+
> File-based routing is powered by the `@tanstack/router-plugin` package, which generates the route tree automatically at build time.
|
|
55
|
+
|
|
56
|
+
## Search Params
|
|
57
|
+
|
|
58
|
+
TanStack Router treats search params as first-class citizens with full type safety:
|
|
59
|
+
|
|
60
|
+
```tsx
|
|
61
|
+
const productsRoute = createRoute({
|
|
62
|
+
getParentRoute: () => rootRoute,
|
|
63
|
+
path: '/products',
|
|
64
|
+
validateSearch: (search: Record<string, unknown>) => {
|
|
65
|
+
return {
|
|
66
|
+
page: Number(search.page) || 1,
|
|
67
|
+
filter: (search.filter as string) || '',
|
|
68
|
+
sort: (search.sort as 'asc' | 'desc') || 'asc',
|
|
69
|
+
}
|
|
70
|
+
},
|
|
71
|
+
component: ProductsPage,
|
|
72
|
+
})
|
|
73
|
+
|
|
74
|
+
function ProductsPage() {
|
|
75
|
+
const { page, filter, sort } = productsRoute.useSearch()
|
|
76
|
+
|
|
77
|
+
return (
|
|
78
|
+
<div>
|
|
79
|
+
<p>Page: {page}, Filter: {filter}, Sort: {sort}</p>
|
|
80
|
+
</div>
|
|
81
|
+
)
|
|
82
|
+
}
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
## Data Loading
|
|
86
|
+
|
|
87
|
+
Routes can define loaders that fetch data before the route renders:
|
|
88
|
+
|
|
89
|
+
```tsx
|
|
90
|
+
const postRoute = createRoute({
|
|
91
|
+
getParentRoute: () => postsRoute,
|
|
92
|
+
path: '$postId',
|
|
93
|
+
loader: async ({ params }) => {
|
|
94
|
+
const post = await fetchPost(params.postId)
|
|
95
|
+
return { post }
|
|
96
|
+
},
|
|
97
|
+
component: PostPage,
|
|
98
|
+
})
|
|
99
|
+
|
|
100
|
+
function PostPage() {
|
|
101
|
+
const { post } = postRoute.useLoaderData()
|
|
102
|
+
return <article>{post.title}</article>
|
|
103
|
+
}
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
| Feature | Description |
|
|
107
|
+
|---|---|
|
|
108
|
+
| Route Trees | Hierarchical route definitions with parent-child relationships |
|
|
109
|
+
| File-Based Routing | Automatic route generation from file system |
|
|
110
|
+
| Search Params | Type-safe URL search parameters with validation |
|
|
111
|
+
| Data Loading | Pre-fetch data before route renders |
|
|
112
|
+
| Pending States | Built-in loading indicators during navigation |
|
|
113
|
+
| Error Boundaries | Per-route error handling |
|
|
114
|
+
|
|
115
|
+
## Path Params
|
|
116
|
+
|
|
117
|
+
Dynamic segments in the path are prefixed with `$`:
|
|
118
|
+
|
|
119
|
+
```tsx
|
|
120
|
+
// Single param
|
|
121
|
+
'/posts/$postId'
|
|
122
|
+
|
|
123
|
+
// Multiple params
|
|
124
|
+
'/users/$userId/posts/$postId'
|
|
125
|
+
|
|
126
|
+
// Splat/catch-all
|
|
127
|
+
'/files/$'
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
> [!WARNING]
|
|
131
|
+
> Unlike some other routers, TanStack Router does not use `:` for dynamic segments. Always use `$` as the prefix.
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: Installation
|
|
3
|
+
description: How to install TanStack Router in your project
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
# Installation
|
|
7
|
+
|
|
8
|
+
## Install the Package
|
|
9
|
+
|
|
10
|
+
```bash
|
|
11
|
+
npm install @tanstack/react-router
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
```bash
|
|
15
|
+
pnpm add @tanstack/react-router
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
```bash
|
|
19
|
+
yarn add @tanstack/react-router
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
```bash
|
|
23
|
+
bun add @tanstack/react-router
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
## Vite Plugin (Recommended)
|
|
27
|
+
|
|
28
|
+
For the best experience with file-based routing and automatic route generation, install the Vite plugin:
|
|
29
|
+
|
|
30
|
+
```bash
|
|
31
|
+
npm install -D @tanstack/router-plugin
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
Then add it to your `vite.config.ts`:
|
|
35
|
+
|
|
36
|
+
```ts
|
|
37
|
+
import { defineConfig } from 'vite'
|
|
38
|
+
import react from '@vitejs/plugin-react'
|
|
39
|
+
import { TanStackRouterVite } from '@tanstack/router-plugin/vite'
|
|
40
|
+
|
|
41
|
+
export default defineConfig({
|
|
42
|
+
plugins: [TanStackRouterVite(), react()],
|
|
43
|
+
})
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
## Devtools
|
|
47
|
+
|
|
48
|
+
Install the devtools for route debugging:
|
|
49
|
+
|
|
50
|
+
```bash
|
|
51
|
+
npm install @tanstack/router-devtools
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
## Requirements
|
|
55
|
+
|
|
56
|
+
- React 18+
|
|
57
|
+
- TypeScript 5.0+ (strongly recommended)
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: TanStack Router Overview
|
|
3
|
+
description: Type-safe routing for React applications with first-class search param support
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
# TanStack Router
|
|
7
|
+
|
|
8
|
+
TanStack Router is a fully type-safe React router with built-in data fetching, stale-while-revalidate caching, and first-class search param APIs.
|
|
9
|
+
|
|
10
|
+
## Why TanStack Router?
|
|
11
|
+
|
|
12
|
+
Traditional routers have limited type safety and treat search params as an afterthought. TanStack Router was built from the ground up with type safety and search params as first-class citizens.
|
|
13
|
+
|
|
14
|
+
## Features
|
|
15
|
+
|
|
16
|
+
- **100% Type-Safe** — Route paths, params, search params, and loaders are all fully typed
|
|
17
|
+
- **Built-in Search Params** — First-class search param management with validation
|
|
18
|
+
- **Data Loading** — Integrated route loaders with stale-while-revalidate caching
|
|
19
|
+
- **Code Splitting** — Automatic route-based code splitting
|
|
20
|
+
- **Nested Layouts** — Nested routes with shared layouts
|
|
21
|
+
- **Devtools** — Dedicated devtools for debugging routes and cache
|
|
22
|
+
|
|
23
|
+
## Basic Example
|
|
24
|
+
|
|
25
|
+
```tsx
|
|
26
|
+
import {
|
|
27
|
+
createRouter,
|
|
28
|
+
createRoute,
|
|
29
|
+
createRootRoute,
|
|
30
|
+
} from '@tanstack/react-router'
|
|
31
|
+
|
|
32
|
+
const rootRoute = createRootRoute({
|
|
33
|
+
component: () => (
|
|
34
|
+
<div>
|
|
35
|
+
<nav>
|
|
36
|
+
<Link to="/">Home</Link>
|
|
37
|
+
<Link to="/about">About</Link>
|
|
38
|
+
</nav>
|
|
39
|
+
<Outlet />
|
|
40
|
+
</div>
|
|
41
|
+
),
|
|
42
|
+
})
|
|
43
|
+
|
|
44
|
+
const indexRoute = createRoute({
|
|
45
|
+
getParentRoute: () => rootRoute,
|
|
46
|
+
path: '/',
|
|
47
|
+
component: () => <div>Welcome Home!</div>,
|
|
48
|
+
})
|
|
49
|
+
|
|
50
|
+
const aboutRoute = createRoute({
|
|
51
|
+
getParentRoute: () => rootRoute,
|
|
52
|
+
path: '/about',
|
|
53
|
+
component: () => <div>About</div>,
|
|
54
|
+
})
|
|
55
|
+
|
|
56
|
+
const routeTree = rootRoute.addChildren([indexRoute, aboutRoute])
|
|
57
|
+
|
|
58
|
+
const router = createRouter({ routeTree })
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
## Type-Safe Search Params
|
|
62
|
+
|
|
63
|
+
```tsx
|
|
64
|
+
const productsRoute = createRoute({
|
|
65
|
+
getParentRoute: () => rootRoute,
|
|
66
|
+
path: '/products',
|
|
67
|
+
validateSearch: (search) => ({
|
|
68
|
+
page: Number(search.page ?? 1),
|
|
69
|
+
filter: (search.filter as string) ?? '',
|
|
70
|
+
sort: (search.sort as 'asc' | 'desc') ?? 'asc',
|
|
71
|
+
}),
|
|
72
|
+
component: ProductsPage,
|
|
73
|
+
})
|
|
74
|
+
```
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: Quick Start
|
|
3
|
+
description: Get up and running with TanStack Router in minutes
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
# Quick Start
|
|
7
|
+
|
|
8
|
+
## File-Based Routing
|
|
9
|
+
|
|
10
|
+
The easiest way to get started is with file-based routing. Create your routes directory:
|
|
11
|
+
|
|
12
|
+
```
|
|
13
|
+
src/
|
|
14
|
+
routes/
|
|
15
|
+
__root.tsx
|
|
16
|
+
index.tsx
|
|
17
|
+
about.tsx
|
|
18
|
+
posts/
|
|
19
|
+
index.tsx
|
|
20
|
+
$postId.tsx
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
### Root Route
|
|
24
|
+
|
|
25
|
+
```tsx
|
|
26
|
+
// src/routes/__root.tsx
|
|
27
|
+
import { createRootRoute, Link, Outlet } from '@tanstack/react-router'
|
|
28
|
+
|
|
29
|
+
export const Route = createRootRoute({
|
|
30
|
+
component: () => (
|
|
31
|
+
<>
|
|
32
|
+
<nav>
|
|
33
|
+
<Link to="/">Home</Link>
|
|
34
|
+
<Link to="/about">About</Link>
|
|
35
|
+
<Link to="/posts">Posts</Link>
|
|
36
|
+
</nav>
|
|
37
|
+
<Outlet />
|
|
38
|
+
</>
|
|
39
|
+
),
|
|
40
|
+
})
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
### Index Route
|
|
44
|
+
|
|
45
|
+
```tsx
|
|
46
|
+
// src/routes/index.tsx
|
|
47
|
+
import { createFileRoute } from '@tanstack/react-router'
|
|
48
|
+
|
|
49
|
+
export const Route = createFileRoute('/')({
|
|
50
|
+
component: () => <div>Welcome Home!</div>,
|
|
51
|
+
})
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
### Dynamic Route
|
|
55
|
+
|
|
56
|
+
```tsx
|
|
57
|
+
// src/routes/posts/$postId.tsx
|
|
58
|
+
import { createFileRoute } from '@tanstack/react-router'
|
|
59
|
+
|
|
60
|
+
export const Route = createFileRoute('/posts/$postId')({
|
|
61
|
+
loader: async ({ params }) => {
|
|
62
|
+
return fetchPost(params.postId)
|
|
63
|
+
},
|
|
64
|
+
component: PostPage,
|
|
65
|
+
})
|
|
66
|
+
|
|
67
|
+
function PostPage() {
|
|
68
|
+
const post = Route.useLoaderData()
|
|
69
|
+
return <div>{post.title}</div>
|
|
70
|
+
}
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
## Data Loading
|
|
74
|
+
|
|
75
|
+
TanStack Router supports route-level data loading with caching:
|
|
76
|
+
|
|
77
|
+
```tsx
|
|
78
|
+
export const Route = createFileRoute('/posts')({
|
|
79
|
+
loader: async () => {
|
|
80
|
+
const posts = await fetch('/api/posts').then((r) => r.json())
|
|
81
|
+
return { posts }
|
|
82
|
+
},
|
|
83
|
+
component: PostsPage,
|
|
84
|
+
})
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
> [!NOTE]
|
|
88
|
+
> Route loaders run in parallel for nested routes, ensuring the fastest possible data loading.
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: インストール
|
|
3
|
+
description: プロジェクトに TanStack Router をインストールする方法
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
# インストール
|
|
7
|
+
|
|
8
|
+
## パッケージのインストール
|
|
9
|
+
|
|
10
|
+
```bash
|
|
11
|
+
npm install @tanstack/react-router
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
```bash
|
|
15
|
+
pnpm add @tanstack/react-router
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
```bash
|
|
19
|
+
yarn add @tanstack/react-router
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
```bash
|
|
23
|
+
bun add @tanstack/react-router
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
## Vite プラグイン(推奨)
|
|
27
|
+
|
|
28
|
+
ファイルベースルーティングと自動ルート生成で最高の体験を得るために、Vite プラグインをインストールしてください:
|
|
29
|
+
|
|
30
|
+
```bash
|
|
31
|
+
npm install -D @tanstack/router-plugin
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
`vite.config.ts` に追加します:
|
|
35
|
+
|
|
36
|
+
```ts
|
|
37
|
+
import { defineConfig } from 'vite'
|
|
38
|
+
import react from '@vitejs/plugin-react'
|
|
39
|
+
import { TanStackRouterVite } from '@tanstack/router-plugin/vite'
|
|
40
|
+
|
|
41
|
+
export default defineConfig({
|
|
42
|
+
plugins: [TanStackRouterVite(), react()],
|
|
43
|
+
})
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
## Devtools
|
|
47
|
+
|
|
48
|
+
ルートデバッグ用の devtools をインストールします:
|
|
49
|
+
|
|
50
|
+
```bash
|
|
51
|
+
npm install @tanstack/router-devtools
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
## 必要要件
|
|
55
|
+
|
|
56
|
+
- React 18+
|
|
57
|
+
- TypeScript 5.0+(強く推奨)
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: TanStack Router 概要
|
|
3
|
+
description: Reactアプリケーション向けの型安全なルーティングライブラリ
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
# TanStack Router
|
|
7
|
+
|
|
8
|
+
TanStack Routerは、Reactアプリケーション向けに設計された完全型安全なルーティングライブラリです。ナビゲーションからサーチパラメータまで、エンドツーエンドの型安全性を提供します。
|
|
9
|
+
|
|
10
|
+
## なぜTanStack Routerを選ぶのか?
|
|
11
|
+
|
|
12
|
+
従来のReactルーティングライブラリは型安全性の面で不十分でした。ルートパスは文字列、パラメータは`any`型、サーチパラメータはまったく検証されません。TanStack Routerはこれらの問題を根本から解決します。
|
|
13
|
+
|
|
14
|
+
## 主な特徴
|
|
15
|
+
|
|
16
|
+
- **100% 型安全** — リンク、ナビゲーション、パラメータ、サーチパラメータすべてが完全に型チェックされる
|
|
17
|
+
- **ファーストクラスのサーチパラメータ** — URLサーチパラメータが検証され型付けされる
|
|
18
|
+
- **組み込みデータローディング** — ルートレベルのデータプリフェッチ
|
|
19
|
+
- **ファイルベースルーティング** — ファイルシステムから型安全なルートツリーを自動生成
|
|
20
|
+
- **ネストされたレイアウト** — ルートのネストで共有レイアウトを作成
|
|
21
|
+
- **コード分割** — ルートごとのコード分割を標準サポート
|
|
22
|
+
|
|
23
|
+
## クイック例
|
|
24
|
+
|
|
25
|
+
```tsx
|
|
26
|
+
import {
|
|
27
|
+
createRootRoute,
|
|
28
|
+
createRoute,
|
|
29
|
+
createRouter,
|
|
30
|
+
Link,
|
|
31
|
+
Outlet,
|
|
32
|
+
} from '@tanstack/react-router'
|
|
33
|
+
|
|
34
|
+
const rootRoute = createRootRoute({
|
|
35
|
+
component: () => (
|
|
36
|
+
<div>
|
|
37
|
+
<nav>
|
|
38
|
+
<Link to="/">ホーム</Link>
|
|
39
|
+
<Link to="/about">アバウト</Link>
|
|
40
|
+
</nav>
|
|
41
|
+
<Outlet />
|
|
42
|
+
</div>
|
|
43
|
+
),
|
|
44
|
+
})
|
|
45
|
+
|
|
46
|
+
const indexRoute = createRoute({
|
|
47
|
+
getParentRoute: () => rootRoute,
|
|
48
|
+
path: '/',
|
|
49
|
+
component: () => <h1>ホームページへようこそ</h1>,
|
|
50
|
+
})
|
|
51
|
+
|
|
52
|
+
const aboutRoute = createRoute({
|
|
53
|
+
getParentRoute: () => rootRoute,
|
|
54
|
+
path: '/about',
|
|
55
|
+
component: () => <h1>私たちについて</h1>,
|
|
56
|
+
})
|
|
57
|
+
|
|
58
|
+
const routeTree = rootRoute.addChildren([indexRoute, aboutRoute])
|
|
59
|
+
const router = createRouter({ routeTree })
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
## TanStack Queryとの連携
|
|
63
|
+
|
|
64
|
+
TanStack RouterはTanStack Queryと相性が抜群です。ルートの`loader`でQueryを使用してデータをプリフェッチできます:
|
|
65
|
+
|
|
66
|
+
```tsx
|
|
67
|
+
const userRoute = createRoute({
|
|
68
|
+
path: '/users/$userId',
|
|
69
|
+
loader: ({ params, context }) =>
|
|
70
|
+
context.queryClient.ensureQueryData({
|
|
71
|
+
queryKey: ['user', params.userId],
|
|
72
|
+
queryFn: () => fetchUser(params.userId),
|
|
73
|
+
}),
|
|
74
|
+
})
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
> [!NOTE]
|
|
78
|
+
> TanStack Routerは単独で使用することも、TanStack Startと組み合わせてフルスタックフレームワークとして使用することもできます。
|