REACT查询如果错误定义自定义行为(基于刷新的新令牌获取新令牌)

发布于 2025-02-10 13:43:55 字数 296 浏览 2 评论 0原文

我试图理解如果我进行了geedeest(查询或突变)=&gt,我如何覆盖React查询的默认行为。它收到401不受影响的。我知道我需要根据我在本地存储中已经拥有的刷新令牌发送请求以获取新的访问令牌。

我每次都可以触发它,只需编写逻辑,如果onerror,然后在新的accessToken中再次尝试请求。

但是我的应用程序有超过1000多个请求,我需要添加此逻辑。我想知道如何默认情况下将其在一个地方进行。大概是中间件或类似的东西(我是新手)。请分享任何合适的解决方案。

先感谢您

I try to understand how could i override default behavior of React Query if for example i made reguest (query or mutation ) => and it recieved 401 Unathorization. I understand that i need to send request to get new access token based on my refresh token which i already have in my localstorage.

I could trigger it every time just write logic if onError and then try request once again with new accessToken.

But my app have more than 1000+ requests that i need to add this logic. And i wonder how i can make it in one place by default. Probably in Middleware or something like this (I'm new in React). Please share any kind of suitable solution.

Thank you in advance

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

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

发布评论

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

评论(1

绝不服输 2025-02-17 13:43:55

现在,推荐的方法是 将这种逻辑与React-Query息息相关,而是使用为您提供数据获取的实际库。如果您使用例如Axios,则提供拦截器。此

The recommended approach right now is to not couple this logic with react-query, but with the actual library that does the data fetching for you. If you use for example axios - it offers interceptors. There are also some good suggestions in this discussion about async retries.

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