Axios vs. Fetch
In the dynamic realm of JavaScript and front-end development, selecting the appropriate tool for HTTP requests is critic...
For further actions, you may consider blocking this person and/or reporting abuse
There are a few extra aspects of the Fetch API that I think folks forget about:
axiosto keep up with security problems, but not with the Fetch API.axiostakes up space in yournode_modulesand bundle, but the Fetch API does not.axiosdoes not.While I know that this isn't the point of your article, I think it's important to note that String Manipulation of URLs is an Anti-Pattern.
In many cases, the biggest argument against using fetch directly is running the same code in node and in the browser. Fetch is not accessible in node, so you would need to use a library like node-fetch in order to access similar functionality on the backend. Rather than taking this route, many REST libraries can run on both sides because they polyfill on the inside.
This is outdated. You can use fetch in recent versions.
Anyway, the argument of having a stable layer in between is still compelling.
Another argument to make is that axios gives you a better thought out API and more options - eg having a progress callback when uploading or downloading large files.
I agree its outdated, however, many companies run with older versions of technologies and shifting the infrastructure requires jumping through hoops. In an ideal scenario, those in charge of infrastructure should keep up with the versions, but unfortunately its not always the case.
We got rid of
node-fetchfrom a large nextjs-project (both server and client side) in a single commit and two files changed:package.jsonservices.ts(the composition root for our DI)The problem is not in the
fetch, the problem is that view directly depends on the implementation isntead of depending on the abstraction.So, detailed research. But I guess it contains a problem, that could be expressed with the simple question: why React components/hooks have to warry about fetch or axios?
React components MUST rely on the abstraction, that MUST be provided on the highest level as it is possible (ideally on the application level, but considering SPA architecture it could be a "page"-level or something like that).
Let's guess we need to display the Books fetched via some rest-like API.
It doesn't mean that we need to write fetch requests inside of the react-components or even hooks.
We need to create an interface (or type):
Then we need to declare correspondent dependency inside of the react-component or hook. For simplicity let's select the context api
then we can use it:
Having this code we can write tests for this components WITHOUT mocking the
fetch.Please note, that component
LibraryBooksdoesn't know anything about the source of the data, it is not aware of authorization, content types, http-respones, base urls, protocols etc.Sure, we need to create function that implements the
IBooksProviderinterface.But we should to do the same.
This function MUST NOT depend directly on the
fetchoraxios. Again, it MUST depend on the abstraction:Where the types
HttpOptionsandHttpResponsecould be as narrow as it allows your project (the backend you are using, or are going to use).Let's guess, the following:
Then we need to create a function
getLibraryBooks:Again, we can write tests for this function. Note, it is not aware of the base url and authorization. Also it assumes that
httpClientdeserializes body according to the response headers.Please note, that for the function
getLibraryBooksany response status except of200and404is unexpected, so it can just throw an error (nothttpClient, but this function)So, now we need to write an implementation for our
IHttpClient.And only now we should choose the fetch or axios. More then, this implementation MUST NOT depend on the
fetchoraxios-instance directly, it must depend on the abstraction, and allow to inject the correspondent instances.The practice shows that such implementation has the same complexity for
fetchandaxios. We had a nextjs-project (usedfetch) and pure back-end nodejs project (usedaxios) and we have a service that must work on both projects. So we had to implement theIHttpClientfor both:fetchandaxiosto allow to use this Service in both projects. Later when we rewrite other services of the back-end project to useIHttpClientinstead of axios, we got rid of the last one, and after node20 LTS released we removed thenode-fetchfrom the both projects.No react-component suffered )
Summarizing:
The title of this article should be modified to say "... in React".
Not all of us use Axios, because not all of us need it.
Fetch works perfectly fine in vanilla JS applications.
Thanks for the great post!!! A lot of details! There are also axios interceptors that are very cool to use π
I have never understood why people insists on installing something for things you can already natively do.
This article is not for beginners.