Better Programming

Advice for programmers.

Follow publication

Member-only story

How To Use Axios in an Optimized and Scalable Way With React

Nilanth
Better Programming
Published in
3 min readJul 13, 2021

city street at night
Photo by Marc-Olivier Jodoin on Unsplash

We can see how to use Axios in an optimized and scalable way in a React app.
When you are building an API-based React app, an HTTP client is the core service that needs to be added to the architecture. There are many HTTP client libraries for React. When deciding which to choose, Axios might be the first choice of most developers.

Axios is an HTTP client library with many advantage features. It can be used in browsers and Node.js. In this article, we will see how to utilize all the advanced Axios features in a scalable and optimized way.

Axios Instance

Creating an Axios instance is more important for a large-scale app, as all the base configuration lies in a single file. A change in the base config can be done easily in a single file and will be reflected anywhere the Axios instance is used. Check out the below code snippet

Based on the above code, we have configured all the base setup using default config, which will be applied to every request which uses the axiosClient instance, as shown in the below request.

Axios Verbs

We can group the Axios HTTP verbs, like GET, POST, DELETE, and PATCH, in the base config file, as below.

Now we can import the custom functions directly wherever we need to make an API request, as in the below code.

Create an account to read the full story.

The author made this story available to Medium members only.
If you’re new to Medium, create a new account to read this story on us.

Or, continue in mobile web

Already have an account? Sign in

Nilanth
Nilanth

Written by Nilanth

Full Stack Developer | Let’s make the web. Faster 🚀 | Tweet me @Nilanth | nilanth.vercel.app | https://buymeacoffee.com/nilanth

Responses (2)

Write a response

You explained everything in a very straightforward and understandable way.
Great job!

Request interceptors are asynchronous by default, which might cause some delay in Axios request execution. To avoid this, we have used synchronous: true.

I think this needs to be clarified a little bit more. Only use synchronous : true if you know that your interceptor code is not making any asynchronous calls.