How to Display Data from API in ReactJS using fetch

share link

by vsasikalabe dot icon Updated: Feb 7, 2023

technology logo
technology logo

Solution Kit Solution Kit  

API stands for Application Programming Interface which is a combination of communication protocols and subroutines used by many programs to communicate between them. A programmer can make use of these API tools to make their program easier and simpler. programmers can develop their software programs in an efficient way using API.

The Fetch API mentioned through the fetch() method allows us to use an HTTP request to the backend. The Fetch API is represented by the response interface to a request. We can call one of the methods of the Response object e.g., text() or JSON () to get the actual data. These methods return the actual data. The JSON () method interface takes a Response object and reads it completely. It returns a promise which resolves the body text as JSON.

In your React application, you should always make sure that your fetch request is in the componentDidMount lifecycle method in a class component or using the use effect hook in a functional component.

There are different of fetching data:

  • By using Fetch API-We can fetch data by using the fetch() method. It will request the server and load the information on the web pages. It will return a promise.
  • By using Axios library-Axios is a javascript library that we use to make HTTP requests the same as fetch(). The difference between these two is in Axios already returns the result as a JSON object, so we don’t need to convert it but in the fetch() method we have to convert the desired result to a JSON object.
  • By using async-await syntax-We use Async-Await as it is an asynchronous technique that is executed through an event loop. Async functions will always return a value. It is the most preferred way of fetching data from an API. then() callbacks return asynchronously resolved data. In the async block, to wait for the promise we will use Await function.
  • By using custom hooks- we use custom hooks when we have a component logic that can be used by other components in our application. It is a React functional component whose name will start with “use” like use fetch. One or more React hooks are used inside the custom hooks. We also have a React-Fetch-Hook for fetching data from API. This library has several types of properties that we can use data it will give us data fetched from API, the error we use for handling errors, and isLoading is used for loading.
  • By using React Query-For obtaining the request operations in our application we use React Query library which helps us to handle complex application caching. It is so simpler to write in React Query than React-Redux. It is executing the pre-fetching of data so that the application can update the data in the background.

Here is an example of how to display a response from a request using Fetch API in Reactjs:

Preview of the output that you will get on running this code from your IDE.


In this solution we use the React library.


Follow the steps carefully to get the output easily.

  1. Install the Node.js and React on your IDE(preferable Visual Studio Code).
  2. Create React Application using npx create-react-app foldername.
  3. cd foldername.
  4. Open the folder in IDE.
  5. Copy the code using "copy" button above and paste it in App.js file(remove the earlier code from App.js).
  6. Open the terminal from IDE.
  7. npm start to run the file.

I hope you found this useful. I have added the link to dependent libraries, version information in the following sections.

I found this code snippet by searching for 'Fetch API in Reactjs' in kandi. You can try any such use case!

Environment Tested

I tested this solution in the following versions. Be mindful of changes when working with other versions.

  1. The solution is created in VS Code 1.73.1 version.
  2. The solution is tested on Nodejs 16.14.2 version.
  3. react 18.2.0 version.

Using this solution, we are able to display the response from request using fetchAPI with simple steps. This process also facilities an easy way to use, hassle-free method to create a hands-on working version of code which would help us to display the response from request using fetchAPI.

Dependent Libraries

reactby facebook

JavaScript doticonstar image 209050 doticonVersion:v18.2.0doticon
License: Permissive (MIT)

The library for web and native user interfaces


            reactby facebook

            JavaScript doticon star image 209050 doticonVersion:v18.2.0doticon License: Permissive (MIT)

            The library for web and native user interfaces

                      You can search for any dependent library on kandi like ' react '.


                      1. For any support on kandi solution kits, please use the chat
                      2. For further learning resources, visit the Open Weaver Community learning page

                      See similar Kits and Libraries