【问题标题】:Update cache of periodical react-query with refetchInterval from other place使用来自其他地方的 refetchInterval 更新定期反应查询的缓存
【发布时间】:2022-11-09 21:49:06
【问题描述】:

对于计数器,我使用 refetchInterval 选项使用 react-query 定期轮询后端:

const { items } = useQuery(
  ["queryKey"],
  requestFn,
  {
    refetchInterval: 5000,
  }
);
...
<Counter>{items.length}</Counter>

唉,当我添加或删除一个项目时,最多需要 5 秒才能反映在计数器中。

如何强制计数器立即更新?

我想在添加/删除例程之后用相同的键触发一次查询,但它不起作用。

addItem().then(() => useQuery(
  ["queryKey"],
  requestFn,
))

【问题讨论】:

    标签: reactjs caching polling react-query cache-invalidation


    【解决方案1】:

    您可能希望在运行突变后执行queryClient.invalidateQueries(["queryKey"]),因为这将立即重新获取新数据。这记录在这里:https://tanstack.com/query/v4/docs/guides/invalidations-from-mutations

    【讨论】:

      猜你喜欢
      • 2020-10-25
      • 1970-01-01
      • 2019-10-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-10-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多