What's the recommended command, library or tool for making API calls within react-router? #13714
-
Hello all, In short, I'm stuck with HTTP 302 redirects being ignored for unauthenticated users. In my case, I expect a redirect happening back to the login page within react-router. Sounds like a basic question. Yet, here is the longer version: when you pick Axios for example, fetch the user resource unauthenticated, it grabs the new location within the HTTP 302 header right-away and does another API call without my will (this within the react-route loader). Axios shouldn't make a subsequent call. The new location should be passed over to react-router to change the page, in my example, back to the login page. This makes me question Axios, whether it's suitable for react-router. I've researched, read the documentation, there is no mention of this use case. To me, it feels like Axios is too bloated and shouldn't do subsequent API calls automatically. Should I go back to the native How do you handle 302 redirects for unauthenticated users in react-router based apps? Is Axios still recommended? Just starting a discussion. Thanks for all your inputs :) |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 4 replies
-
Do you perform these API calls in client or server loader? |
Beta Was this translation helpful? Give feedback.
-
API calls are from the client side, within React effects, for example. Yes, I realised 302 was wrong. Express.js default |
Beta Was this translation helpful? Give feedback.
Loaders can return
redirect
/replace
responses. In your specific case you can just handle your auth stuff in a client loader and straight up throw actual errors and filter for them in the nearest error boundary, no need to bother with statuses and deserialization of server errors.