rtkquery Update缓存
我不明白如何在组件中没有其他请求的情况下更新缓存,在这种情况下,我调用端点getentity,以便将高速缓存键端点getentity + arg分配给代码中的每个实体。
现在,我以这种方式连接缓存键,
const Entity = ({ id }) => {
const res = apiQuery.useGetEntityQuery(id);
return <div>{JSON.stringify(res.data)}</div>;
};
const Output = ({ Entities }) => {
return (
<div>
{Entities &&
Entities instanceof Array &&
Entities.map(({ id }) => {
return <Entity key={id} id={id} />;
})}
</div>
);
};
每个元素调用API Query.usegetentityQuery(ID)是多么正确?
是否有更正确的方法可以使用适用于这种情况的高速缓存?
是否可以使下面的代码起作用?为此需要什么,还是不必这样做?
const Output = ({ Entities }) => {
return (
<div>
{Entities &&
Entities instanceof Array &&
Entities.map(({ id, value }) => {
return (
<div key={id}>
{id}:{value}
</div>
);
})}
</div>
);
};
I did not understand how to update the cache without additional requests in the component, in this case I call endpoint getEntity so that the cache key endpoint getEntity + arg is assigned to each entity in the code.
I wrote a simple example
Now I attach the cache key in this way,
const Entity = ({ id }) => {
const res = apiQuery.useGetEntityQuery(id);
return <div>{JSON.stringify(res.data)}</div>;
};
const Output = ({ Entities }) => {
return (
<div>
{Entities &&
Entities instanceof Array &&
Entities.map(({ id }) => {
return <Entity key={id} id={id} />;
})}
</div>
);
};
how correct would it be to call api Query.useGetEntityQuery(id) for each element?
Is there a more correct way to work with the cache applicable to this case?
Is it possible to make the code below work and what is needed for this, or is it not necessary to do this?
const Output = ({ Entities }) => {
return (
<div>
{Entities &&
Entities instanceof Array &&
Entities.map(({ id, value }) => {
return (
<div key={id}>
{id}:{value}
</div>
);
})}
</div>
);
};
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在RTK查询中,每个缓存条目都是完全分开的,因此,如果您真的需要,则必须为其编写逻辑 - 尽管大多数情况下,其他请求都不会受到伤害,这是设计用于使用的主要方式。
如果您想这样做,请阅读手动缓存更新
In RTK Query, each cache entry is completely separate, so you have to write the logic for that yourself if you really need to - although most of the time an additional request does not hurt and that is the main way it is designed to being used.
If you want to do it though, read Manual Cache Updates