13 Tweets 12 reads Jul 04, 2022
Managing API calls in React.
Thread ๐Ÿงต
You often need to make an API call in your React application.
In this thread, we will discuss four widely used ways to make an API call in React.
1๏ธโƒฃ Fetch
The most convenient way to make an API call in React is to use Fetch web API.
It returns a promise that resolves into the response.
You don't need to install or import any third-party app for using Fetch.
It comes with native JavaScript.
You can call it inside the `useEffect` hook from React.
Here's the typical syntax of Fetch.
2๏ธโƒฃ Axios
Axios is the most popular third-party app for making API calls.
It allows you to make HTTP requests using a promise-based HTTP client.
You can install it inside your React app using the following command.
`npm install axios`
`axios.get()` function allows you to make a GET request.
`axios.put()` function allows you to make a PUT request. And so on.
Here's the typical syntax of Axios.
3๏ธโƒฃ SWR
SWR is a modern library of React hooks created by Vercel.
It has a lot of extra features like:
โ€ข built-in caching
โ€ข reusable data fetching
โ€ข request deduplication
โ€ข revalidation on focus
โ€ข network discovery, etc
To use SWR, we need to install it first.
`npm install swr`
Here's the typical syntax of SWR.
4๏ธโƒฃ Apollo Client
You can use Apollo Client if you're consuming GraphQL API.
It provides a comprehensive tool for GraphQL data fetching.
Read this thread on RapidAPI Guides.
RapidAPI.com
With that said, this is the end of this thread.
Follow @Rapid_API to read more exclusive content. ๐Ÿ’™ ๐Ÿš€

Loading suggestions...