DEV Community

Discussion on: useAxios: React hook for any Axios call

Collapse
 
kwhitejr profile image
Kevin White

Mainly it is syntactical sugar around common needs, such as hooks for isLoading, isError, and the built-in request cancellation.

Collapse
 
sroehrl profile image
neoan • Edited

I see. More syntactical consistency rather than sugar then it that case?
But still, mentioned problems wouldn't exist if API calls happened on the store level. In your components, you would still use hooks depending on the capabilities of your state management. If you combine that with a layer for an axios instance that handles headers & authorisation, then you remove the need for a custom config each time. It's a way cleaner and more maintainable code base then.
Don't get me wrong,there is nothing wrong with your approach, but at the end you produce a lot of lines of code whenever you make something as standard as an API call (which easily happens hundreds of times in a medium sized application)

Thread Thread
 
kwhitejr profile image
Kevin White

I dig it. Do you have an example repo that implements that pattern from which I could learn?

Thread Thread
 
sroehrl profile image
neoan

Unfortunately I have nothing I can share, no. But it shouldn't be hard:
We utilize axios interceptors to check if a used is authenticated and attach the JWT token accordingly. With hookState , recoil or redux (whatever you use), you can then assign the transactions accordingly.