REACT查询如果错误定义自定义行为(基于刷新的新令牌获取新令牌)
我试图理解如果我进行了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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
现在,推荐的方法是 将这种逻辑与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.