How To Use Axios in an Optimized and Scalable Way With React
Tags: react, javascript, webdev, beginners
Published: 2021-07-14 14:22:46

We can see how to use [Axios](https://axios-http.com/) 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](https://nodejs.org/en/). 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

{% gist https://gist.github.com/Nilanth/3d6fd06f4f66e63d2877f90ff9b15e19 %}

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.

{% gist https://gist.github.com/Nilanth/2cb5884e7c144799299f080495c973d8 %}



## Axios Verbs

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

{% gist https://gist.github.com/Nilanth/a15e1334bba54ba898129c8e4f1d38ec %}

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

{% gist https://gist.github.com/Nilanth/33de507287af8300571c2cc9c62b9cfd %}

## Timeout

The Axios timeout option allows setting the request timeout in milliseconds. It is easy to configure timeout in Axios. It can be defined in the base config itself, as we saw before.

```
timeout: 2000 // Request will be timeout after 2 seconds.
```

## Intercept Request

Using a request intercept, you can write or execute before it is sent. Check out the below code snippet.

{% gist https://gist.github.com/Nilanth/983ddc05590c33e9bf36f06979e8374a %}

Here we have added the `contentType` header before the request is made. Request interceptors are asynchronous by default, which might cause some delay in Axios request execution. To avoid this, we have used `synchronous: true`.

## Intercept Response

Using Request intercept you can write or execute before the response reaches `then`. Response interceptors can be used to log out the user on token expiry (401 status) or you can refresh the token and make the failed request again to make the user stay on the same page, for good UX.

{% gist https://gist.github.com/Nilanth/52077ebaf55edbd165fe77ae55146375 %}

## Upload Progress

You can track upload progress in Axios very easily using the `onUploadProgress` option. `onUploadProgress` allows handling of progress events for uploads. This can be used to show the upload percentage live to the user, to acknowledge to them the upload is in progress. Check out the below code.

{% gist
https://gist.github.com/Nilanth/77821579f54bc9e7d0c4454410826084 %}

## Resources

[Axios GitHub repository](https://github.com/axios/axios)

## Conclusion

Axios is a great HTTP client package for React and its community. I hope you have found this useful. Thank you for reading.

Need to learn more? Feel free to connect on [Twitter](https://twitter.com/Nilanth).

**You can support me [by buying me a coffee](https://buymeacoffee.com/nilanth) ☕**

## eBook

[Debugging ReactJS Issues with ChatGPT: 50 Essential Tips and Examples](https://nilanth.gumroad.com/l/ztehh)

[ReactJS Optimization Techniques and Development Resources](https://nilanth.gumroad.com/l/NYkdN)

## More Blogs

1. [Redux Toolkit - The Standard Way to Write Redux
](https://dev.to/nilanth/redux-toolkit-the-standard-way-to-write-redux-2g32) 
2. [5 Packages to Optimize and Speed Up Your React App During Development](https://dev.to/nilanth/5-packages-to-optimize-and-speed-up-your-react-app-during-development-4h5f)
3. [15 Custom Hooks to Make your React Component Lightweight](https://dev.to/nilanth/15-custom-hooks-to-make-your-react-component-lightweight-17cd)
4. [10 Ways to Host Your React App For Free](https://dev.to/nilanth/10-ways-to-host-your-react-app-for-free-27ga)
5. [React 18 Alpha: A Quick Overview](https://dev.to/nilanth/react-18-alpha-a-quick-overview-349n)
6. [Redux Auth Starter: A Zero Config CRA Template](https://dev.to/nilanth/redux-auth-starter-a-zero-config-cra-template-9c0)