TanStack Query 为每个查询函数提供一个 AbortSignal 实例。当查询过时或变为非活动状态时,此 signal 将被中止。这意味着所有查询都是可取消的,并且如果需要,您可以在查询函数内部响应取消。最好的部分是,它允许您继续使用正常的 async/await 语法,同时获得自动取消的所有好处。
AbortController API 在大多数运行时环境中都可用,但如果您的运行时环境不支持它,则需要提供一个 polyfill。有几种可用的。
默认情况下,在 Promise 解析之前卸载或变为未使用的查询不会被取消。这意味着在 Promise 解析后,结果数据将在缓存中可用。如果您已开始接收查询,但在完成之前卸载了组件,这将很有用。如果您再次挂载组件并且查询尚未被垃圾回收,则数据将可用。
但是,如果您使用了 AbortSignal,则 Promise 将被取消(例如,中止 fetch),因此,查询也必须被取消。取消查询将导致其状态恢复到其先前的状态。
const query = useQuery(() => {
queryKey: ['todos'],
queryFn: async ({ signal }) => {
const todosResponse = await fetch('/todos', {
// 将信号传递给一个 fetch
signal,
})
const todos = await todosResponse.json()
const todoDetails = todos.map(async ({ details }) => {
const response = await fetch(details, {
// 或者将其传递给多个
signal,
})
return response.json()
})
return Promise.all(todoDetails)
},
})
const query = useQuery(() => {
queryKey: ['todos'],
queryFn: async ({ signal }) => {
const todosResponse = await fetch('/todos', {
// 将信号传递给一个 fetch
signal,
})
const todos = await todosResponse.json()
const todoDetails = todos.map(async ({ details }) => {
const response = await fetch(details, {
// 或者将其传递给多个
signal,
})
return response.json()
})
return Promise.all(todoDetails)
},
})
import axios from 'axios'
const query = useQuery(() => {
queryKey: ['todos'],
queryFn: ({ signal }) =>
axios.get('/todos', {
// 将信号传递给 `axios`
signal,
}),
})
import axios from 'axios'
const query = useQuery(() => {
queryKey: ['todos'],
queryFn: ({ signal }) =>
axios.get('/todos', {
// 将信号传递给 `axios`
signal,
}),
})
import axios from 'axios'
const query = useQuery(() => {
queryKey: ['todos'],
queryFn: ({ signal }) => {
// 为此请求创建一个新的 CancelToken 源
const CancelToken = axios.CancelToken
const source = CancelToken.source()
const promise = axios.get('/todos', {
// 将源令牌传递给您的请求
cancelToken: source.token,
})
// 如果 TanStack Query 发出中止信号,则取消请求
signal?.addEventListener('abort', () => {
source.cancel('查询已被 TanStack Query 取消')
})
return promise
},
})
import axios from 'axios'
const query = useQuery(() => {
queryKey: ['todos'],
queryFn: ({ signal }) => {
// 为此请求创建一个新的 CancelToken 源
const CancelToken = axios.CancelToken
const source = CancelToken.source()
const promise = axios.get('/todos', {
// 将源令牌传递给您的请求
cancelToken: source.token,
})
// 如果 TanStack Query 发出中止信号,则取消请求
signal?.addEventListener('abort', () => {
source.cancel('查询已被 TanStack Query 取消')
})
return promise
},
})
const query = useQuery(() => {
queryKey: ['todos'],
queryFn: ({ signal }) => {
return new Promise((resolve, reject) => {
var oReq = new XMLHttpRequest()
oReq.addEventListener('load', () => {
resolve(JSON.parse(oReq.responseText))
})
signal?.addEventListener('abort', () => {
oReq.abort()
reject()
})
oReq.open('GET', '/todos')
oReq.send()
})
},
})
const query = useQuery(() => {
queryKey: ['todos'],
queryFn: ({ signal }) => {
return new Promise((resolve, reject) => {
var oReq = new XMLHttpRequest()
oReq.addEventListener('load', () => {
resolve(JSON.parse(oReq.responseText))
})
signal?.addEventListener('abort', () => {
oReq.abort()
reject()
})
oReq.open('GET', '/todos')
oReq.send()
})
},
})
可以在客户端 request 方法中设置 AbortSignal。
const client = new GraphQLClient(endpoint)
const query = useQuery(() => {
queryKey: ['todos'],
queryFn: ({ signal }) => {
client.request({ document: query, signal })
},
})
const client = new GraphQLClient(endpoint)
const query = useQuery(() => {
queryKey: ['todos'],
queryFn: ({ signal }) => {
client.request({ document: query, signal })
},
})
可以在 GraphQLClient 构造函数中设置 AbortSignal。
const query = useQuery(() => {
queryKey: ['todos'],
queryFn: ({ signal }) => {
const client = new GraphQLClient(endpoint, {
signal,
})
return client.request(query, variables)
},
})
const query = useQuery(() => {
queryKey: ['todos'],
queryFn: ({ signal }) => {
const client = new GraphQLClient(endpoint, {
signal,
})
return client.request(query, variables)
},
})
您可能希望手动取消查询。例如,如果请求需要很长时间才能完成,您可以允许用户单击取消按钮来停止请求。为此,您只需调用 queryClient.cancelQueries({ queryKey }),这将取消查询并将其恢复到其先前的状态。如果您已使用了传递给查询函数的 signal,TanStack Query 还将额外取消 Promise。
const query = useQuery(() => {
queryKey: ['todos'],
queryFn: async ({ signal }) => {
const resp = await fetch('/todos', { signal })
return resp.json()
},
})
const queryClient = useQueryClient()
return (
<button
onClick={(e) => {
e.preventDefault()
queryClient.cancelQueries({ queryKey: ['todos'] })
}}
>
取消
</button>
)
const query = useQuery(() => {
queryKey: ['todos'],
queryFn: async ({ signal }) => {
const resp = await fetch('/todos', { signal })
return resp.json()
},
})
const queryClient = useQueryClient()
return (
<button
onClick={(e) => {
e.preventDefault()
queryClient.cancelQueries({ queryKey: ['todos'] })
}}
>
取消
</button>
)
与 Suspense 钩子一起使用时,取消不起作用:useSuspenseQuery、useSuspenseQueries 和 useSuspenseInfiniteQuery。