docs-i18n 0.8.1 → 0.8.3
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 +32 -32
- package/package.json +1 -1
- package/template/app/routes/$lang.$project.$version.docs.$.tsx +2 -1
- package/template/app/routes/$lang.$project.$version.docs.framework.$framework.$.tsx +2 -0
- package/template/app/routes/$lang.$project.$version.docs.tsx +2 -1
- package/template/app/routes/$lang.$project.docs.$.tsx +2 -1
- package/template/app/routes/$lang.$project.docs.tsx +2 -1
- package/template/app/routes/$lang.docs.$.tsx +2 -1
- package/template/app/routes/$lang.docs.framework.$framework.$.tsx +2 -0
- package/template/app/routes/$lang.docs.tsx +2 -1
- package/template/app/utils/content-loader.ts +13 -2
- package/template/app/utils/docs.server.ts +17 -15
- 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 +335 -0
- package/template/content/docs-i18n/en/cli.md +13 -1
- package/template/content/docs-i18n/en/configuration.md +350 -0
- package/template/content/docs-i18n/en/deployment.md +222 -0
- package/template/content/docs-i18n/en/getting-started.md +189 -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,120 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: Queries
|
|
3
|
+
description: Learn how queries work in TanStack Query
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
# Queries
|
|
7
|
+
|
|
8
|
+
A query is a declarative dependency on an asynchronous source of data that is tied to a unique key. A query can be used with any Promise-based method (including GET and POST methods) to fetch data from a server.
|
|
9
|
+
|
|
10
|
+
## Query Basics
|
|
11
|
+
|
|
12
|
+
To subscribe to a query in your components, call the query hook with at least a unique key and a function that returns a promise:
|
|
13
|
+
|
|
14
|
+
<!-- ::start:react -->
|
|
15
|
+
|
|
16
|
+
```tsx
|
|
17
|
+
import { useQuery } from '@tanstack/react-query'
|
|
18
|
+
|
|
19
|
+
function Example() {
|
|
20
|
+
const { data, isLoading, error } = useQuery({
|
|
21
|
+
queryKey: ['todos'],
|
|
22
|
+
queryFn: fetchTodoList,
|
|
23
|
+
})
|
|
24
|
+
}
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
<!-- ::end:react -->
|
|
28
|
+
|
|
29
|
+
<!-- ::start:vue -->
|
|
30
|
+
|
|
31
|
+
```vue
|
|
32
|
+
<script setup>
|
|
33
|
+
import { useQuery } from '@tanstack/vue-query'
|
|
34
|
+
|
|
35
|
+
const { data, isLoading, error } = useQuery({
|
|
36
|
+
queryKey: ['todos'],
|
|
37
|
+
queryFn: fetchTodoList,
|
|
38
|
+
})
|
|
39
|
+
</script>
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
<!-- ::end:vue -->
|
|
43
|
+
|
|
44
|
+
<!-- ::start:solid -->
|
|
45
|
+
|
|
46
|
+
```tsx
|
|
47
|
+
import { createQuery } from '@tanstack/solid-query'
|
|
48
|
+
|
|
49
|
+
function Example() {
|
|
50
|
+
const query = createQuery(() => ({
|
|
51
|
+
queryKey: ['todos'],
|
|
52
|
+
queryFn: fetchTodoList,
|
|
53
|
+
}))
|
|
54
|
+
}
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
<!-- ::end:solid -->
|
|
58
|
+
|
|
59
|
+
## Query Keys
|
|
60
|
+
|
|
61
|
+
Query keys are used to uniquely identify a query. They can be as simple as a string, or as complex as an array of many strings and nested objects:
|
|
62
|
+
|
|
63
|
+
```ts
|
|
64
|
+
// A simple string key
|
|
65
|
+
useQuery({ queryKey: ['todos'], ... })
|
|
66
|
+
|
|
67
|
+
// A key with variables
|
|
68
|
+
useQuery({ queryKey: ['todo', todoId], ... })
|
|
69
|
+
|
|
70
|
+
// A key with an object
|
|
71
|
+
useQuery({ queryKey: ['todos', { type: 'done', page: 1 }], ... })
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
## Query Functions
|
|
75
|
+
|
|
76
|
+
A query function can be literally any function that returns a promise. The promise that is returned should either resolve the data or throw an error:
|
|
77
|
+
|
|
78
|
+
```ts
|
|
79
|
+
const { data } = useQuery({
|
|
80
|
+
queryKey: ['todos'],
|
|
81
|
+
queryFn: async () => {
|
|
82
|
+
const response = await fetch('/api/todos')
|
|
83
|
+
if (!response.ok) {
|
|
84
|
+
throw new Error('Network response was not ok')
|
|
85
|
+
}
|
|
86
|
+
return response.json()
|
|
87
|
+
},
|
|
88
|
+
})
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
## Query States
|
|
92
|
+
|
|
93
|
+
A query can be in one of the following states at any given moment:
|
|
94
|
+
|
|
95
|
+
- **`isPending`** — The query has no data yet
|
|
96
|
+
- **`isError`** — The query encountered an error
|
|
97
|
+
- **`isSuccess`** — The query was successful and data is available
|
|
98
|
+
|
|
99
|
+
```tsx
|
|
100
|
+
function Todos() {
|
|
101
|
+
const { data, isPending, isError, error } = useQuery({
|
|
102
|
+
queryKey: ['todos'],
|
|
103
|
+
queryFn: fetchTodos,
|
|
104
|
+
})
|
|
105
|
+
|
|
106
|
+
if (isPending) return <span>Loading...</span>
|
|
107
|
+
if (isError) return <span>Error: {error.message}</span>
|
|
108
|
+
|
|
109
|
+
return (
|
|
110
|
+
<ul>
|
|
111
|
+
{data.map((todo) => (
|
|
112
|
+
<li key={todo.id}>{todo.title}</li>
|
|
113
|
+
))}
|
|
114
|
+
</ul>
|
|
115
|
+
)
|
|
116
|
+
}
|
|
117
|
+
```
|
|
118
|
+
|
|
119
|
+
> [!NOTE]
|
|
120
|
+
> In TanStack Query v5, `isLoading` has been renamed to `isPending` for consistency.
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: Installation
|
|
3
|
+
description: How to install TanStack Query in your project
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
# Installation
|
|
7
|
+
|
|
8
|
+
TanStack Query is available as a package on npm for each supported framework.
|
|
9
|
+
|
|
10
|
+
## Install the Package
|
|
11
|
+
|
|
12
|
+
Choose the package for your framework:
|
|
13
|
+
|
|
14
|
+
### React
|
|
15
|
+
|
|
16
|
+
```bash
|
|
17
|
+
npm install @tanstack/react-query
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
```bash
|
|
21
|
+
pnpm add @tanstack/react-query
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
```bash
|
|
25
|
+
yarn add @tanstack/react-query
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
```bash
|
|
29
|
+
bun add @tanstack/react-query
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
### Vue
|
|
33
|
+
|
|
34
|
+
```bash
|
|
35
|
+
npm install @tanstack/vue-query
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
### Solid
|
|
39
|
+
|
|
40
|
+
```bash
|
|
41
|
+
npm install @tanstack/solid-query
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
### Svelte
|
|
45
|
+
|
|
46
|
+
```bash
|
|
47
|
+
npm install @tanstack/svelte-query
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
### Angular
|
|
51
|
+
|
|
52
|
+
```bash
|
|
53
|
+
npm install @tanstack/angular-query-experimental
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
## Requirements
|
|
57
|
+
|
|
58
|
+
- TypeScript 4.7+ (for type inference)
|
|
59
|
+
- React 18+ / Vue 3+ / Solid 1.6+ / Svelte 4+
|
|
60
|
+
|
|
61
|
+
## Devtools
|
|
62
|
+
|
|
63
|
+
We recommend also installing the devtools for a better development experience:
|
|
64
|
+
|
|
65
|
+
```bash
|
|
66
|
+
npm install @tanstack/react-query-devtools
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
> [!NOTE]
|
|
70
|
+
> Devtools are currently only available for React. Community devtools exist for other frameworks.
|
|
71
|
+
|
|
72
|
+
## CDN
|
|
73
|
+
|
|
74
|
+
For prototyping, you can load TanStack Query from a CDN:
|
|
75
|
+
|
|
76
|
+
```html
|
|
77
|
+
<script src="https://unpkg.com/@tanstack/react-query/build/umd/index.production.js"></script>
|
|
78
|
+
```
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: TanStack Query Overview
|
|
3
|
+
description: Powerful asynchronous state management for TS/JS, React, Vue, Solid, Svelte & Angular
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
# TanStack Query
|
|
7
|
+
|
|
8
|
+
TanStack Query (formerly React Query) makes fetching, caching, synchronizing, and updating server state in your web applications a breeze.
|
|
9
|
+
|
|
10
|
+
## Motivation
|
|
11
|
+
|
|
12
|
+
Most core web frameworks do not come with an opinionated way of fetching or updating data in a holistic way. Developers end up building either meta-frameworks which encapsulate strict opinions about data-fetching, or they invent their own ways of fetching data. This usually means cobbling together component-based state and side-effects, or using more general purpose state management libraries to store and provide asynchronous data throughout their apps.
|
|
13
|
+
|
|
14
|
+
## Features
|
|
15
|
+
|
|
16
|
+
- **Transport/protocol/backend agnostic** data fetching (REST, GraphQL, promises, whatever!)
|
|
17
|
+
- **Auto Caching** with request deduplication
|
|
18
|
+
- **Automatic Refetching** with stale-while-revalidate strategy
|
|
19
|
+
- **Window Focus Refetching** to keep data current
|
|
20
|
+
- **Polling/Realtime** queries with configurable intervals
|
|
21
|
+
- **Parallel & Dependent Queries** for complex data requirements
|
|
22
|
+
- **Mutations** with optimistic updates
|
|
23
|
+
- **Pagination & Infinite Scroll** out of the box
|
|
24
|
+
- **Dedicated Devtools** for debugging
|
|
25
|
+
|
|
26
|
+
## Supported Frameworks
|
|
27
|
+
|
|
28
|
+
TanStack Query supports the following frameworks:
|
|
29
|
+
|
|
30
|
+
```ts
|
|
31
|
+
// React
|
|
32
|
+
import { useQuery } from '@tanstack/react-query'
|
|
33
|
+
|
|
34
|
+
// Vue
|
|
35
|
+
import { useQuery } from '@tanstack/vue-query'
|
|
36
|
+
|
|
37
|
+
// Solid
|
|
38
|
+
import { createQuery } from '@tanstack/solid-query'
|
|
39
|
+
|
|
40
|
+
// Svelte
|
|
41
|
+
import { createQuery } from '@tanstack/svelte-query'
|
|
42
|
+
|
|
43
|
+
// Angular
|
|
44
|
+
import { injectQuery } from '@tanstack/angular-query-experimental'
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
## Quick Example
|
|
48
|
+
|
|
49
|
+
```tsx
|
|
50
|
+
import { useQuery } from '@tanstack/react-query'
|
|
51
|
+
|
|
52
|
+
function App() {
|
|
53
|
+
const { data, isLoading, error } = useQuery({
|
|
54
|
+
queryKey: ['todos'],
|
|
55
|
+
queryFn: () => fetch('/api/todos').then(res => res.json()),
|
|
56
|
+
})
|
|
57
|
+
|
|
58
|
+
if (isLoading) return <div>Loading...</div>
|
|
59
|
+
if (error) return <div>Error: {error.message}</div>
|
|
60
|
+
|
|
61
|
+
return (
|
|
62
|
+
<ul>
|
|
63
|
+
{data.map(todo => (
|
|
64
|
+
<li key={todo.id}>{todo.title}</li>
|
|
65
|
+
))}
|
|
66
|
+
</ul>
|
|
67
|
+
)
|
|
68
|
+
}
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
> [!NOTE]
|
|
72
|
+
> TanStack Query v5 is the latest version and includes significant improvements over v4 including a simplified API and better TypeScript support.
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: Quick Start
|
|
3
|
+
description: Get up and running with TanStack Query in minutes
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
# Quick Start
|
|
7
|
+
|
|
8
|
+
This guide will help you get started with TanStack Query as quickly as possible.
|
|
9
|
+
|
|
10
|
+
## Set Up the Provider
|
|
11
|
+
|
|
12
|
+
Wrap your application with `QueryClientProvider` and pass a `QueryClient` instance:
|
|
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
|
+
## Your First Query
|
|
29
|
+
|
|
30
|
+
Use the `useQuery` hook to fetch data:
|
|
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>Loading...</span>
|
|
42
|
+
if (error) return <span>Error: {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
|
+
## Your First Mutation
|
|
61
|
+
|
|
62
|
+
Use `useMutation` to create, update, or delete data:
|
|
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
|
+
// Invalidate and refetch
|
|
78
|
+
queryClient.invalidateQueries({ queryKey: ['todos'] })
|
|
79
|
+
},
|
|
80
|
+
})
|
|
81
|
+
|
|
82
|
+
return (
|
|
83
|
+
<button onClick={() => mutation.mutate({ title: 'New Todo' })}>
|
|
84
|
+
Add Todo
|
|
85
|
+
</button>
|
|
86
|
+
)
|
|
87
|
+
}
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
## Adding Devtools
|
|
91
|
+
|
|
92
|
+
Add the React Query Devtools for debugging:
|
|
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
|
+
> Devtools are automatically excluded from production builds.
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: インストール
|
|
3
|
+
description: プロジェクトに TanStack Query をインストールする方法
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
# インストール
|
|
7
|
+
|
|
8
|
+
TanStack Query は、サポートされている各フレームワーク向けの npm パッケージとして利用できます。
|
|
9
|
+
|
|
10
|
+
## パッケージのインストール
|
|
11
|
+
|
|
12
|
+
フレームワークに合わせたパッケージを選択してください:
|
|
13
|
+
|
|
14
|
+
### React
|
|
15
|
+
|
|
16
|
+
```bash
|
|
17
|
+
npm install @tanstack/react-query
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
```bash
|
|
21
|
+
pnpm add @tanstack/react-query
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
```bash
|
|
25
|
+
yarn add @tanstack/react-query
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
```bash
|
|
29
|
+
bun add @tanstack/react-query
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
### Vue
|
|
33
|
+
|
|
34
|
+
```bash
|
|
35
|
+
npm install @tanstack/vue-query
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
### Solid
|
|
39
|
+
|
|
40
|
+
```bash
|
|
41
|
+
npm install @tanstack/solid-query
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
### Svelte
|
|
45
|
+
|
|
46
|
+
```bash
|
|
47
|
+
npm install @tanstack/svelte-query
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
### Angular
|
|
51
|
+
|
|
52
|
+
```bash
|
|
53
|
+
npm install @tanstack/angular-query-experimental
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
## 必要要件
|
|
57
|
+
|
|
58
|
+
- TypeScript 4.7+(型推論のため)
|
|
59
|
+
- React 18+ / Vue 3+ / Solid 1.6+ / Svelte 4+
|
|
60
|
+
|
|
61
|
+
## Devtools
|
|
62
|
+
|
|
63
|
+
より良い開発体験のために、devtools のインストールもお勧めします:
|
|
64
|
+
|
|
65
|
+
```bash
|
|
66
|
+
npm install @tanstack/react-query-devtools
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
> [!NOTE]
|
|
70
|
+
> Devtools は現在 React のみ対応しています。他のフレームワーク向けにはコミュニティ製の devtools があります。
|
|
71
|
+
|
|
72
|
+
## CDN
|
|
73
|
+
|
|
74
|
+
プロトタイピングには、CDN から TanStack Query を読み込めます:
|
|
75
|
+
|
|
76
|
+
```html
|
|
77
|
+
<script src="https://unpkg.com/@tanstack/react-query/build/umd/index.production.js"></script>
|
|
78
|
+
```
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: TanStack Query 概要
|
|
3
|
+
description: TS/JS、React、Vue、Solid、Svelte、Angular向けの強力な非同期状態管理
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
# TanStack Query
|
|
7
|
+
|
|
8
|
+
TanStack Query(旧称 React Query)を使えば、Webアプリケーションにおけるデータの取得、キャッシュ、同期、更新が簡単に行えます。
|
|
9
|
+
|
|
10
|
+
## モチベーション
|
|
11
|
+
|
|
12
|
+
ほとんどのWebフレームワークには、データの取得や更新について統一的な方法が用意されていません。開発者は独自のデータ取得方法を考案するか、メタフレームワークを構築する必要があります。これは通常、コンポーネントベースの状態と副作用を組み合わせるか、汎用的な状態管理ライブラリを使って非同期データを管理することを意味します。
|
|
13
|
+
|
|
14
|
+
## 特徴
|
|
15
|
+
|
|
16
|
+
- **トランスポート/プロトコル/バックエンド非依存** のデータ取得(REST、GraphQL、Promiseなど何でも対応!)
|
|
17
|
+
- **自動キャッシュ** とリクエストの重複排除
|
|
18
|
+
- **自動再取得** — stale-while-revalidate戦略
|
|
19
|
+
- **ウィンドウフォーカス再取得** でデータを最新に保つ
|
|
20
|
+
- **ポーリング/リアルタイム** クエリ(間隔設定可能)
|
|
21
|
+
- **並列・依存クエリ** で複雑なデータ要件に対応
|
|
22
|
+
- **ミューテーション** — 楽観的更新をサポート
|
|
23
|
+
- **ページネーション・無限スクロール** を標準サポート
|
|
24
|
+
- **専用DevTools** でデバッグを支援
|
|
25
|
+
|
|
26
|
+
## サポートされるフレームワーク
|
|
27
|
+
|
|
28
|
+
TanStack Queryは以下のフレームワークをサポートしています:
|
|
29
|
+
|
|
30
|
+
```ts
|
|
31
|
+
// React
|
|
32
|
+
import { useQuery } from '@tanstack/react-query'
|
|
33
|
+
|
|
34
|
+
// Vue
|
|
35
|
+
import { useQuery } from '@tanstack/vue-query'
|
|
36
|
+
|
|
37
|
+
// Solid
|
|
38
|
+
import { createQuery } from '@tanstack/solid-query'
|
|
39
|
+
|
|
40
|
+
// Svelte
|
|
41
|
+
import { createQuery } from '@tanstack/svelte-query'
|
|
42
|
+
|
|
43
|
+
// Angular
|
|
44
|
+
import { injectQuery } from '@tanstack/angular-query-experimental'
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
## クイック例
|
|
48
|
+
|
|
49
|
+
```tsx
|
|
50
|
+
import { useQuery } from '@tanstack/react-query'
|
|
51
|
+
|
|
52
|
+
function App() {
|
|
53
|
+
const { data, isLoading, error } = useQuery({
|
|
54
|
+
queryKey: ['todos'],
|
|
55
|
+
queryFn: () => fetch('/api/todos').then(res => res.json()),
|
|
56
|
+
})
|
|
57
|
+
|
|
58
|
+
if (isLoading) return <div>読み込み中...</div>
|
|
59
|
+
if (error) return <div>エラー:{error.message}</div>
|
|
60
|
+
|
|
61
|
+
return (
|
|
62
|
+
<ul>
|
|
63
|
+
{data.map(todo => (
|
|
64
|
+
<li key={todo.id}>{todo.title}</li>
|
|
65
|
+
))}
|
|
66
|
+
</ul>
|
|
67
|
+
)
|
|
68
|
+
}
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
> [!NOTE]
|
|
72
|
+
> TanStack Query v5は最新バージョンであり、APIの簡素化やTypeScriptサポートの改善など、大幅な改良が含まれています。
|
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: 变更操作
|
|
3
|
+
description: 了解如何使用 TanStack Query 的变更操作创建、更新和删除数据
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
# 变更操作
|
|
7
|
+
|
|
8
|
+
与查询不同,变更操作通常用于创建/更新/删除数据或执行服务端副作用。为此,TanStack Query 导出了 `useMutation` Hook。
|
|
9
|
+
|
|
10
|
+
## 基本变更操作
|
|
11
|
+
|
|
12
|
+
```tsx
|
|
13
|
+
import { useMutation, useQueryClient } from '@tanstack/react-query'
|
|
14
|
+
|
|
15
|
+
function AddTodo() {
|
|
16
|
+
const queryClient = useQueryClient()
|
|
17
|
+
|
|
18
|
+
const mutation = useMutation({
|
|
19
|
+
mutationFn: (newTodo: { title: string }) => {
|
|
20
|
+
return fetch('/api/todos', {
|
|
21
|
+
method: 'POST',
|
|
22
|
+
body: JSON.stringify(newTodo),
|
|
23
|
+
headers: { 'Content-Type': 'application/json' },
|
|
24
|
+
}).then((res) => res.json())
|
|
25
|
+
},
|
|
26
|
+
onSuccess: () => {
|
|
27
|
+
// 使待办事项列表缓存失效并重新获取
|
|
28
|
+
queryClient.invalidateQueries({ queryKey: ['todos'] })
|
|
29
|
+
},
|
|
30
|
+
})
|
|
31
|
+
|
|
32
|
+
return (
|
|
33
|
+
<button
|
|
34
|
+
onClick={() => mutation.mutate({ title: 'New Todo' })}
|
|
35
|
+
disabled={mutation.isPending}
|
|
36
|
+
>
|
|
37
|
+
{mutation.isPending ? '添加中...' : '添加待办事项'}
|
|
38
|
+
</button>
|
|
39
|
+
)
|
|
40
|
+
}
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
## 变更状态
|
|
44
|
+
|
|
45
|
+
在任意时刻,变更操作只能处于以下状态之一:
|
|
46
|
+
|
|
47
|
+
| 状态 | 描述 |
|
|
48
|
+
|---|---|
|
|
49
|
+
| `isIdle` | 变更操作处于空闲或初始/重置状态 |
|
|
50
|
+
| `isPending` | 变更操作正在执行 |
|
|
51
|
+
| `isError` | 变更操作遇到错误 |
|
|
52
|
+
| `isSuccess` | 变更操作成功,数据可用 |
|
|
53
|
+
|
|
54
|
+
## 乐观更新
|
|
55
|
+
|
|
56
|
+
乐观更新允许您在服务器确认变更之前更新 UI,提供更流畅的用户体验:
|
|
57
|
+
|
|
58
|
+
```tsx
|
|
59
|
+
const mutation = useMutation({
|
|
60
|
+
mutationFn: updateTodo,
|
|
61
|
+
onMutate: async (newTodo) => {
|
|
62
|
+
// 取消正在进行的重新获取,以避免覆盖乐观更新
|
|
63
|
+
await queryClient.cancelQueries({ queryKey: ['todos'] })
|
|
64
|
+
|
|
65
|
+
// 保存之前的值快照
|
|
66
|
+
const previousTodos = queryClient.getQueryData(['todos'])
|
|
67
|
+
|
|
68
|
+
// 乐观地更新为新值
|
|
69
|
+
queryClient.setQueryData(['todos'], (old: Todo[]) => [
|
|
70
|
+
...old,
|
|
71
|
+
{ ...newTodo, id: Date.now() },
|
|
72
|
+
])
|
|
73
|
+
|
|
74
|
+
// 返回包含快照值的上下文对象
|
|
75
|
+
return { previousTodos }
|
|
76
|
+
},
|
|
77
|
+
onError: (_err, _newTodo, context) => {
|
|
78
|
+
// 出错时回滚到之前的值
|
|
79
|
+
queryClient.setQueryData(['todos'], context?.previousTodos)
|
|
80
|
+
},
|
|
81
|
+
onSettled: () => {
|
|
82
|
+
// 无论成功或失败都重新获取
|
|
83
|
+
queryClient.invalidateQueries({ queryKey: ['todos'] })
|
|
84
|
+
},
|
|
85
|
+
})
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
> [!NOTE]
|
|
89
|
+
> `onMutate` 处理函数在变更函数之前运行,并接收相同的变量。这是执行乐观更新的最佳位置。
|
|
90
|
+
|
|
91
|
+
## 重试
|
|
92
|
+
|
|
93
|
+
默认情况下,TanStack Query 不会重试失败的变更操作。您可以为每个变更操作单独配置:
|
|
94
|
+
|
|
95
|
+
```tsx
|
|
96
|
+
const mutation = useMutation({
|
|
97
|
+
mutationFn: addTodo,
|
|
98
|
+
retry: 3, // 失败的变更操作重试 3 次
|
|
99
|
+
})
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
## 变更回调
|
|
103
|
+
|
|
104
|
+
`useMutation` Hook 支持多个回调选项:
|
|
105
|
+
|
|
106
|
+
```tsx
|
|
107
|
+
useMutation({
|
|
108
|
+
mutationFn: addTodo,
|
|
109
|
+
onMutate: (variables) => {
|
|
110
|
+
// 在变更函数触发之前调用
|
|
111
|
+
// 接收变更函数将接收的相同变量
|
|
112
|
+
},
|
|
113
|
+
onError: (error, variables, context) => {
|
|
114
|
+
// 变更操作遇到错误时调用
|
|
115
|
+
},
|
|
116
|
+
onSuccess: (data, variables, context) => {
|
|
117
|
+
// 变更操作成功时调用
|
|
118
|
+
},
|
|
119
|
+
onSettled: (data, error, variables, context) => {
|
|
120
|
+
// 无论成功还是失败都会调用
|
|
121
|
+
},
|
|
122
|
+
})
|
|
123
|
+
```
|
|
124
|
+
|
|
125
|
+
> [!WARNING]
|
|
126
|
+
> `useMutation` 上的回调会在每个使用它的组件实例中触发。要确保副作用只执行一次,请使用 `mutate` 函数上的回调,或将变更操作提取到共享 Hook 中。
|