Perform an action in case of an error and run the same request once again #321
-
Hi! I would like to refresh JWT token when API returns 401 error. SWR has Example: <SWRConfig
value={{
onErrorRetry: async (
error,
key,
option,
revalidate,
{ retryCount }
) => {
if (retryCount && retryCount >= 2) return;
if (error.status === 404) return;
if (error.status === 401) {
// custom logic
}
setTimeout(
() =>
revalidate({ retryCount: retryCount ? retryCount + 1 : 1 }),
1000
);
},
}}
>
// ...
</SWRConfig> Can I do something similar using react-query? Maybe there's a better way to do it? 🤔 |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
If you're using axios - use interceptors. If not - consider using it :) |
Beta Was this translation helpful? Give feedback.
-
The token-query project was created to address this use case. |
Beta Was this translation helpful? Give feedback.
If you're using axios - use interceptors. If not - consider using it :)
I've migrated to React Query without even touching tokens refresh code.