NFT Atlanta

I recently had the pleasure of going to the first NFT Atlanta show this past Tuesday. There were many speakers with some really big names in web3 and creativity. The event was focused on many…

Smartphone

独家优惠奖金 100% 高达 1 BTC + 180 免费旋转




Effortlessly control all network calls in your react application

When your react app start expanding, network calls are likely to be scattered all across components. Collecting them at one place makes it much easier to scale and manage calls to target api endpoints. It also makes it easier to manipulate requests as well as responses and get them ready to consume in our application.

Apart from inbuilt network functionalities in JS, we have myriad third party packages with varying levels of ease of use and power/control.

Axios provides an elegant way to implement this.

I create a helper file called axiosService.jsand another helper file called credentials.js(where we handle all credentials related functions).

First, credentials.js to manage credentials:

This file is sort of self-explanatory with comments highlighting functions. Basically I check:

Once credentials helper is set up, I can then set up our axiosService.js as below:

As you can see, I have extracted all API endpoints in a single, easily accessible place, and have managed to effortlessly (or with very less effort!) insert credentials in every request made to API.

Now I can import axiosService.js where needed, and make calls via axiosService.api while passing appropriate params.

I can also define appropriate actions or check network status etc. once we detect request errors.

Similar to request interceptors, I can also use response interceptors and extract and process responses received from API.

Below is a simple axiosResponseInterceptor function that handles these errors.

This piece of code will load appropriate page depending on response.

We can also call other functions to preprocess responses before our application consume it.

Add a comment

Related posts:

FOOD INSECURITY IN CANADA

Nutrition is important for healthy life. Many people are still hungry around the world even though there is mass production of food. This is because of unhealthy food production. Even in developed…

When I met Myself

This article was most uncomfortable for me to publish. I am a private person and prefer not to talk about what’s going on in my life and this makes writing this article more of a tough decision for…

CWFF Shuttle Service

CWFF is almost here and we could not be more excited to welcome you to Bloomingburg! CWFF will be offering Shuttle Service to/from select locations to the campground starting Saturday at 10:30 AM and…