Member-only story
Clean API Call With React Hooks
Make API calls more consistent throughout the whole application with concise, clean, and reusable code.
Calling APIs is the most common thing to do in any modern web application. When it comes to talking with an API, we must do many repetitive things like getting data from an API call, handling the success or error case, and so on.
We always have to do those tedious tasks when calling tens of hundreds of API calls. We can handle those things efficiently by putting a higher level of abstraction over those barebone API calls, whereas in some small applications, sometimes we don’t even care.
The problem comes when we start adding new features on top of the existing features without handling the API calls in an efficient and reusable manner. In that case, for all of those API calls related repetitions, we end up with a lot of repetitive code across the whole application.
In React, we have different approaches for calling an API. Nowadays, we mostly use React hooks. With React hooks, it’s possible to handle API calls in a very clean and consistent way throughout the application in spite of whatever the application size is. So let’s see how we can make a clean and reusable API calling layer using React hooks for a simple web application.