API call in React JS

API integration is the most important part of the React JS application. So today we will show you how to fetch data via API call in React JS.

How to consume a RESTful API in React, Fetching API Data with React.JS, Consuming REST APIs With React.js, Introduction to API Calls With React and Axios, React API Call example, reactjs api call example.

Way to fetch data using API call in React JS

  1. Create startup react app
  2. Use fetch to make API call
  3. Output

1. Create startup react application

First you have to create react application. Please refer below link to create startup app.

https://www.cluemediator.com/create-react-application

2. Use fetch to make API call

Step 1: Add below bootstrap css link in index.html to use bootstrap design in application.

Step 2: Use fetch method to make API calls and set data into the react state. You have to define state variable for it and bind method in constructor.

Find more details to handle events in React JS.

https://www.cluemediator.com/handle-events-react

Here we used loading variable to indicate API call is ongoing. For understanding purpose, we used setTimeout after getting API response so we can get the idea of loading.

Step 3: Add button and table to manage API call on button click and display fetched data into the table.

We will call getUserList method on button click event. To manage button text and disabled attribute via loading state variable.

Now, iterate the userList to display records in table and also use image attribute to display as avatar.

3. Output

Output - API call in React JS - Clue Mediator
Output – API call in React JS – Clue Mediator

Demo & Source Code

Github Repository StackBlitz Project
If you found value in this article,
you can support us by buying me a coffee! ☕

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *