React查询乐观更新导致闪烁更新
我对React查询有一个问题,如果用户按按钮太快按下按钮,触发突变,即使返回API调用,正确的值也会在屏幕上闪烁并更改,即使在尝试取消它们时也会返回API调用。我注意到此问题也发生在官方React查询示例以进行乐观更新。这是 fivese 我 我对那里发生了问题。
export const useIncreaseCount = () => {
const queryClient = useQueryClient()
return useMutation(
() => {
const cart = queryClient.getQueryData('cart') as Cart
return setCart(cart)
},
{
onMutate: async (cartItemId: string) => {
await queryClient.cancelQueries('cart')
const previousCart = queryClient.getQueryData('cart') as Cart
queryClient.setQueryData(
'cart',
increaseCount(previousCart, cartItemId)
)
return { previousCart }
},
onError: (error, _cartItem, context) => {
console.log('error mutating cart', error)
if (!context) return
queryClient.setQueryData('cart', context.previousCart)
},
onSuccess: () => {
queryClient.invalidateQueries('cart')
},
}
)
}
我正在考虑拒绝使用UsiseIncreaseCount的电话,但是随后onMutate
将被拒绝,我不想要。理想情况下,仅将拒绝API电话。 React查询中是否内置了内置的方式?
I'm having an issue with React Query where if a user presses a button too fast, triggering a mutation, the correct value flashes and changes on the screen as the API calls are returned, even when attempting to cancel them. I notice this problem also happens in the official React Query example for optimistic updates. Here's a video I took of the problem happening there.
export const useIncreaseCount = () => {
const queryClient = useQueryClient()
return useMutation(
() => {
const cart = queryClient.getQueryData('cart') as Cart
return setCart(cart)
},
{
onMutate: async (cartItemId: string) => {
await queryClient.cancelQueries('cart')
const previousCart = queryClient.getQueryData('cart') as Cart
queryClient.setQueryData(
'cart',
increaseCount(previousCart, cartItemId)
)
return { previousCart }
},
onError: (error, _cartItem, context) => {
console.log('error mutating cart', error)
if (!context) return
queryClient.setQueryData('cart', context.previousCart)
},
onSuccess: () => {
queryClient.invalidateQueries('cart')
},
}
)
}
I'm thinking of debouncing the call to use useIncreaseCount, but then onMutate
will get debounced, and I don't want that. Ideally just the API call would be debounced. Is there a built in way in React Query to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题来自以下事实:即使该突变的不同调用仍在运行,每个
onsuccess
回调 queryclient.invalidatequeries 即使。不执行此操作是用户代码的责任。我看到两种方法:ref
跟踪正在进行的突变量(inmutate
inmutate ,insettled ),然后仅调用
queryclient.invalidatequeries
如果计数器为零。!queryClient.ismutating(key)
也应起作用。The problem come from the fact that every
onSuccess
callback callsqueryClient.invalidateQueries
, even though a different invocation of the mutation is still running. It's the responsibility of the user code to not do that. I see two ways:ref
(increment inonMutate
, decrement inonSettled
), then only callqueryClient.invalidateQueries
if the counter is zero.!queryClient.isMutating(key)
should also work.