Different Ways to fetch data from API in Reactjs
Tags: api, react, javascript, webdev
Published: 2023-03-26 18:19:52

<h1>Introduction</h1>
<ul>
<li>In this blog, we will cover the basics of Web API.
</li>
<li>We will try to learn different ways to fetch data from API.
</li>
<li>Let’s begin without any further delay.
</li>
</ul>
<h2>Web APIs
<p>API stands for Application Programming Interface. The most popular Web Api used is a Representational state transfer API or RESTful API. Web APIs are used to fetch data from a database and save data back to the database. Using Web APIs requires the use of HTTP request methods there are different HTTP request methods like GET,POST,PUT,DELETE, etc. Let’s have a brief description of the mentioned HTTP request.

<li>GET: Used to request data from an endpoint
<li>POST: Sends data to an endpoint
<li>DELETE: Remove data from an endpoint.
<li>PUT: Update a record or data value at an endpoint.

<hr>

<h2>Ways of Fetching Data from API
<p>There are different of fetching data:

<li>By using Fetch API
<li>By using Axios library
<li>By using async-await syntax
<li>By using custom hooks
<li>By using React Query

<hr>

<h2>1. Fetch API
<p>We can fetch data by using javascript fetch() method. It will request sever and load the information on the web pages. It will return a promise.

Let’s start with the example. We will create a fetchData() method. It will call fetch() the method with the given URL then converts the result to a JSON object.

```react
import React, { useEffect, useState } from "react";

function App() { 
  const [user, setUser] = useState([]);

  const fetchData = () => {
    return fetch("https://jsonplaceholder.typicode.com/users")
          .then((response) => response.json())
          .then((data) => setUser(data));
  }

  useEffect(() => {
    fetchData();
  },[])

  return (
    <main>
      <h1>User List</h1>
      <ul>
        {user && user.length > 0 && user.map((userObj, index) => (
            <li key={userObj.id}>{userObj.name}</li>
          ))}
      </ul>
    </main>
  );
}

export default App;
```
<h2>2. Axios
<p>Axios is a javascript library that we use to make HTTP requests same as fetch(). There is a difference between these two as in fetch() we have to convert the result to a JSON object but in Axios it already returns the result as a JSON object, so we don’t need to convert it.

Let’s take the example of Axios library. First, we have to install the Axios library then we have to import Axios from Axios.

```react
npm install axios
```
<hr>
```react
import axios from "axios";
const fetchData = () => {
    return axios.get("https://jsonplaceholder.typicode.com/users")
          .then((response) => setUser(response.data));
  }

  useEffect(() => {
    fetchData();
  },[])
```
<h2>3. Using async and await
<p>We use Async-Await as it is an asynchronous technique that is operated via an event loop. Async functions will always return a value. It is the preferred way of fetching the data from an API as it enables us to remove our .then() callbacks and return asynchronously resolved data. In the async block, we can use Await function to wait for the promise.

```react
async function fetchData() {
    try {
      const response = await axios.get("https://jsonplaceholder.typicode.com/users")
      setUser(response.data)
    } catch (error) {
      console.error(error);
    }
  }

  useEffect(() => {
    fetchData();
  },[])
```
<h2>4. Custom Hooks
<p>Let’s have a brief understanding that what are custom hooks. So, we use custom hooks when we have a component logic that can be used by other components in our application. It is basically a React functional component whose name will start with “use” like useFetch. Custom Hooks can use one or more React hooks inside them.

We also have a React library React-Fetch-Hook for fetching data from API. This library has several properties that we can use like data it will give us data fetched from API, the error we use this for handling errors, and isLoading is used for loading.

Firstly we have to install that library for use that by

```react
npm i react-fetch-hook
```
<hr>
```react
import useFetch from "react-fetch-hook"

const {data} = useFetch("https://jsonplaceholder.typicode.com/users");
console.log(data);
```
Now, we will create our custom hook useFetch.

useFetch.js

```react
import { useEffect, useState } from "react";
import axios from "axios";

const useFetch = (method, url, body) => {
  const [isLoading, setIsLoading] = useState(false);
  const [apiData, setApiData] = useState([]);
  const [apiError, setApiError] = useState('');

  useEffect(() => {
    setIsLoading(true);
    const fetchData = async () => {
      try {
        const response = await axios({
          method: method,
          url: url,
          data: body
        });
        const data = await response?.data;

        setApiData(data);
        setIsLoading(false);
      } catch (error) {
        setApiError(error);
        setIsLoading(false);
      }
    };

    fetchData();
  }, [url, method, body]);

  return { isLoading, apiData, apiError };
};

export default useFetch;
```

App.js

```react
import useFetch from "../src/useFetch";

function App() { 
  const { isLoading, apiError, apiData } = useFetch(
    "GET",
    "https://jsonplaceholder.typicode.com/users",
    {}
  );
```
<h2>5. React Query
<p>For optimizing the request operations in our application we use React Query library which helps us to handle complex application caching. It is far simpler to write in React Query than React-Redux. It performs pre-fetching of data so that the application can update stale data in the background.

So for using React Query first we have to install React Query library by.

```react
npm i react-query
```

So to configure react-query in our React.js application, we need to wrap our components with the QueryClientProvider component. We will do configuration in our root directory file. 

index.js

```react

import React from "react";
import ReactDOM from "react-dom";
import { QueryClient, QueryClientProvider } from "react-query";
import App from "./App";

const queryClient = new QueryClient();

const rootElement = document.getElementById("root");
ReactDOM.render(
  <React.StrictMode>
    <QueryClientProvider client={queryClient}>
      <App />
    </QueryClientProvider>
  </React.StrictMode>,
  rootElement
);
```

<p>Let’s see how we fetch data by useQuery
First, we have to import useQuery from react-query. The useQuery hook returns some states that we can use in our application like isLoading, data, error, Is Error,

```react
import React, { useState } from "react";
import { useQuery } from "react-query";
import axios from "axios";

function App() {
  async function fetchUser() {
    const { data } = await axios.get(
      "https://jsonplaceholder.typicode.com/users"
    );
    return data;
  }

  const { data, error, isError, isLoading } = useQuery("getUser", fetchUser);

  return (
    <main>
      <h1>User List</h1>
      <ul>
        {data &&
          data.length > 0 &&
          data.map((userObj, index) => (
            <li key={userObj.id}>{userObj.name}</li>
          ))}
      </ul>
    </main>
  );
}

export default App;
```
We will create a functional component and import useQuery in that. Then we will create an async function fetchUser() that will return an array of users.

For updating data, we use useMutation hook
So, we use a mutation hook for doing operations like Create, Update, Delete in our components. We will pass the payload as parameters in our mutate function after our async call is done, we will be passed to onSuccess where we can invalidate query that we used for fetching data so that we can get updated data after update into the database.

First, we will see how to use useMutation,

```react
import React, { useState } from "react";
import { useQuery, useMutation } from "react-query";
import axios from "axios";

function App() {
  async function addUser(payload) {
    return await axios.post(
      "https://jsonplaceholder.typicode.com/users",payload
    );
  }

  const {mutate : createNewUser} = useMutation(addUser, {
    onSuccess: () => {
      console.log('success')
    },
  });

  return (
    <main>
      <h1>User List</h1>
      <button onClick={() => {
          createNewUser({
            id: Date.now(),
            userName: 'New User',
          });
        }}>Add User</button>
    </main>
  );
}

export default App;
```

So now we will invalidate our fetch query in onSuccess. First, we have to declare a new query client to have access to the cache.

```react
import React, { useState } from "react";
import { useQuery, useMutation ,useQueryClient} from "react-query";
import axios from "axios";

function App() {
  const queryClient = useQueryClient();

  async function addUser(payload) {
    return await axios.post(
      "https://jsonplaceholder.typicode.com/users",payload
    );
  }


  const {mutate : createNewUser} = useMutation(addUser, {
    onSuccess: () => {
      queryClient.invalidateQueries('getUser');
    },
  });
```

<p>So, in invalidateQueries, we are passing fetch query name (‘getUser’) that will fetch the data.

<h1>Conclusion
<p>In this blog, we have covered different API fetch practices.

<p>In our upcoming blog, we will try to learn about React Hooks.

<p>For more interesting blogs visit- StatusNeo Blogs

<p>Thanks for reading, Ba-Bye for now.