在多个组件中使用RTK查询的数据

发布于 2025-01-24 19:29:13 字数 574 浏览 2 评论 0原文

我在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 技术交流群。

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

发布评论

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

评论(1

压抑⊿情绪 2025-01-31 19:29:13

我不能只接触到Redux-Toolkit商店吗?

那与不检查错误一样,是同一回事吗?

最后:如果您知道它将在其他地方处理,那么您当然不需要检查它。只需检查data!==未定义的

Can't I just reach out to the Redux-toolkit store?

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.

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