反应查询突变不刷新



我有另一个取回"SpecificList在另一个组件,但当帖子执行它不取回它只在rewindowfocus上取回?原因是什么呢?

const { mutate,isLoading:isAdding } = useMutation((values)=>handleAddValue(values),
{
onSuccess:()=> queryClient.invalidateQueries("specificList")
},
)
async function handleAddValue(values){
return await ShoppingListAPI.post("/addToShoppingList",{
product:values.newValue,
list:values.listId
}) 
}
if(isAdding){ return <p>Loading...</p> }
if (isLoading) { return <p>Loading...</p> }
return (
<Box>
{data.products &&
<Autocomplete onChange={(event, newValue) => mutate({newValue,listId})
} options={data.products} getOptionLabel={(option) => option.name} renderInput={(params) => <TextField {...params} label="Products" />}></Autocomplete>
}
</Box>
)

原因是什么?

在没有看到整个代码/codesandbox复制的情况下很难判断。一些需要注意的事情是:

  • 匹配查询键(大写/小写)
  • 确保queryClient是稳定的:要么在你的应用程序组件之外创建,要么在它内部的状态或refinstance上创建。否则,重新渲染将创建一个新的客户端,丢弃缓存。
  • 可能你的突变不成功,所以没有调用onSuccess

最新更新