重用全球状态的反应查询方式是什么?

发布于 2025-02-08 17:26:08 字数 113 浏览 0 评论 0原文

在redux/context API中,我们用来获取数据并在全局状态中保存一次。然后,我们可以通过应用程序重复使用全局状态。例如:持有用户ID,令牌等的用户auth状态。我想知道,做同一件事的反应查询方式是什么。

In redux/context api, we used to fetch the data and save in the global state once. Then we could reuse the global state throuhout the app.For ex: USER auth state that holds user id,token etc. I wanted to know that what is the react query way to do the same thing.

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

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

发布评论

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

评论(2

陌生 2025-02-15 17:26:08

您只需在要访问数据的任何地方调用usequery。该缓存在全球范围内可用,就像Redux Store在全球范围内可用一样。

我已经广泛地博客,例如:
https://tkdodo.eu/blog/blog/react-query-as-as-a-a -State-Manager

You can just call useQuery wherever you want to access the data. The cache is globally available, just like the redux store is globally available.

I've blogged about this extensively, e.g. here:
https://tkdodo.eu/blog/react-query-as-a-state-manager

墨小沫ゞ 2025-02-15 17:26:08

您始终可以使用USEQUERY并在任何组件中访问状态,甚至可以通过使用USEMUNT进行修改状态。但是,请记住,React查询本质上是服务器状态管理工具。如果用作客户状态管理,最终可能会导致问题。例如,在刷新浏览器后,您将把保存的数据丢失到USEQUERY中。因此,我认为如果需要,使用Redux/Context API等客户端状态管理会更合适。

You can always use useQuery and access the state in any component, or even modify the state with useMutation. However, keep in mind that react query is essentially a server state management tool. If used as a client state management, it may eventually lead to issues. For example, after refreshing the browser, you would lose the data saved into useQuery. So I think it would be more appropriate to use a client state management like redux/context api with react query if needed.

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