以下代码片段简要展示了 React Query 的 3 个核心概念:
如需查看完整功能示例,请参考我们的 简单 StackBlitz 示例
import {
useQuery,
useMutation,
useQueryClient,
QueryClient,
QueryClientProvider,
} from '@tanstack/react-query'
import { getTodos, postTodo } from '../my-api'
// 创建客户端
const queryClient = new QueryClient()
function App() {
return (
// 将客户端提供给应用
<QueryClientProvider client={queryClient}>
<Todos />
</QueryClientProvider>
)
}
function Todos() {
// 访问客户端
const queryClient = useQueryClient()
// 查询
const query = useQuery({ queryKey: ['todos'], queryFn: getTodos })
// 变更
const mutation = useMutation({
mutationFn: postTodo,
onSuccess: () => {
// 使缓存失效并重新获取
queryClient.invalidateQueries({ queryKey: ['todos'] })
},
})
return (
<div>
<ul>{query.data?.map((todo) => <li key={todo.id}>{todo.title}</li>)}</ul>
<button
onClick={() => {
mutation.mutate({
id: Date.now(),
title: 'Do Laundry',
})
}}
>
添加待办
</button>
</div>
)
}
render(<App />, document.getElementById('root'))
import {
useQuery,
useMutation,
useQueryClient,
QueryClient,
QueryClientProvider,
} from '@tanstack/react-query'
import { getTodos, postTodo } from '../my-api'
// 创建客户端
const queryClient = new QueryClient()
function App() {
return (
// 将客户端提供给应用
<QueryClientProvider client={queryClient}>
<Todos />
</QueryClientProvider>
)
}
function Todos() {
// 访问客户端
const queryClient = useQueryClient()
// 查询
const query = useQuery({ queryKey: ['todos'], queryFn: getTodos })
// 变更
const mutation = useMutation({
mutationFn: postTodo,
onSuccess: () => {
// 使缓存失效并重新获取
queryClient.invalidateQueries({ queryKey: ['todos'] })
},
})
return (
<div>
<ul>{query.data?.map((todo) => <li key={todo.id}>{todo.title}</li>)}</ul>
<button
onClick={() => {
mutation.mutate({
id: Date.now(),
title: 'Do Laundry',
})
}}
>
添加待办
</button>
</div>
)
}
render(<App />, document.getElementById('root'))
这三个概念构成了 React Query 的大部分核心功能。文档的后续章节将详细讲解每个核心概念。