在多个组件中使用RTK查询的数据
我在API中具有动态含量的React天然中有Mulitple页面。加载第一页后,使用RTK查询获取数据。因此,我检查是否有任何错误,加载状态(显示活动指标)以及是否存在任何数据。
我从文档中知道,当我在另一个组件中再次调用RTK查询挂钩时,网络调用不会触发,而是将返回缓存的数据。
但这是我的问题。我也应该检查所有其他组件中的错误/加载状态吗?我的意思是,这是我需要一遍又一遍地投入每个组件的几行代码:
const { data: { data } = {}, isLoading, isError } = useGetOnboardingDataQuery();
if (isLoading || isError || !data) {
return <Loading isLoading={isLoading} isError={isError || !data} />;
}
return PageComponent....
我认为这不是必需的吗?您如何处理?我不能只是去Redux-Toolkit商店吗?
这可能是一个愚蠢的问题,因为我是Redux-Toolkit和RTK查询的新手。只想听听一些意见
I have mulitple pages in React Native with dynamic content from the api. When the first page is loaded, the data is fetched using RTK query. So I check for any errors, the loading state (to show an Activity indicator) and if there is any data present.
I know from the docs that when I call the RTK query hook again in another component, the network call won't be triggered and instead the cached data will be returned.
But here is my question. Should I check for errors/loading state... in all other components too? I mean, it's a few lines of code that I'll need to put in every component over and over and over again:
const { data: { data } = {}, isLoading, isError } = useGetOnboardingDataQuery();
if (isLoading || isError || !data) {
return <Loading isLoading={isLoading} isError={isError || !data} />;
}
return PageComponent....
I don't think it's necessary? How do you handle this? Can't I just reach out to the Redux-toolkit store?
This may be a stupid question because I'm new to redux-toolkit and RTK query. Just want to hear some opinions
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
那与不检查错误一样,是同一回事吗?
最后:如果您知道它将在其他地方处理,那么您当然不需要检查它。只需检查
data!==未定义的
。That would be the same thing as just not checking for errors, would it?
In the end: if you know it will be handled somewhere else, of course you don't need to check it. Just check that
data !== undefined
.