RTK查询 - 删除cacheentryAdded时删除缓存数据
当前,我们有一个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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
实际上,首先不会更新任何内容。
updateQueryResults
具有与product
inincoper
或normalcreateSlice
case reducers相同的规则:您可以在<<<<代码>状态(在这种情况下为草稿),但是您无法重新分配变量本身。如果您想这样做,则必须相反。
以相同的方式,您
也可以,但这不会删除完整的高速缓存条目,它将仅将
Data
将其设置为null
。 (未定义的
不起作用!)仅一旦没有更多组件使用它,才会删除缓存条目(通过不将其与
useQuery
)一起使用 - 然后将60秒后自动删除。actually does not update anything here in the first place.
updateQueryResults
has the same rules asproduce
fromimmer
or normalcreateSlice
case reducers: you can modify the object instate
(ordraft
in this case), but you cannot reassign the variable itself. If you want to do that, you have toinstead.
In the same fashion, you can
too, but that will not remove the full cache entry, it will only set the
data
tonull
. (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.