RTK查询 - 删除cacheentryAdded时删除缓存数据

发布于 2025-01-27 09:04:58 字数 316 浏览 1 评论 0原文

当前,我们有一个API端点,该端点通过ID请求单个“组”。

我们设置了一个WebSocket订阅,在Oncacheentryadd的定义中,我们处理该组已更新或删除的情况。

当我们从Websocket收到更新消息时,我们会触发以下内容;

updateCachedData((draft) => {
  draft = response;
}

如预期的那样,它更新了条目。

但是,如果要完全删除条目,我们应该使用什么方法?从Websocket删除消息时,我认为我可以简单地将草稿设置为未定义,但事实并非如此。

Currently we have an api endpoint that requests a single 'Group' via ID.

We have a WebSocket subscription set up, and in the onCacheEntryAdded definition, we handle cases where that Group is updated, or deleted.

When we receive an update message from the websocket, we trigger the following;

updateCachedData((draft) => {
  draft = response;
}

Which updates the entry, as expected.

However, what is the approach we should use if we want to remove the entry entirely? Upon 'delete' messages from the websocket, I would assume I could simply set draft as undefined, but that doesn't seem to be the case.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

白鸥掠海 2025-02-03 09:04:58
updateCachedData((draft) => {
  draft = response;
}

实际上,首先不会更新任何内容。

updateQueryResults具有与product in incoper或normal createSlice case reducers相同的规则:您可以在<<<<代码>状态(在这种情况下为草稿),但是您无法重新分配变量本身。如果您想这样做,则必须

updateCachedData((draft) => {
  return response;
}

相反。

以相同的方式,您

updateCachedData((draft) => {
  return null;
}

也可以,但这不会删除完整的高速缓存条目,它将仅将Data将其设置为null。 (未定义的不起作用!)

仅一旦没有更多组件使用它,才会删除缓存条目(通过不将其与useQuery)一起使用 - 然后将60秒后自动删除。

updateCachedData((draft) => {
  draft = response;
}

actually does not update anything here in the first place.

updateQueryResults has the same rules as produce from immer or normal createSlice case reducers: you can modify the object in state (or draft in this case), but you cannot reassign the variable itself. If you want to do that, you have to

updateCachedData((draft) => {
  return response;
}

instead.

In the same fashion, you can

updateCachedData((draft) => {
  return null;
}

too, but that will not remove the full cache entry, it will only set the data to null. (undefined won't work!)

The cache entry will only be removed once there is no more component using it (by not using it with useQuery) - and then it will be removed automatically after 60 seconds.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文