Using Axios to fetch data and display it in Table in React

Active3 hr before
Viewed126 times

11 Answers


Transform Axios Response Data,2. Sign Up For a Free Account on RapidAPI,3. Subscribe to the Alpha Vantage API

If they answer, the promise is ‘resolved’, and if they don’t answer, the promise is ‘rejected’. Errors can be thrown when Promises are rejected.


It’s a simple concept, but implementing Promises can be a bit tricky. Consequently, developers typically use tools like axios or fetch.


If they answer, the promise is ‘resolved’, and if they don’t answer, the promise is ‘rejected’. Errors can be thrown when Promises are rejected.


It’s a simple concept, but implementing Promises can be a bit tricky. Consequently, developers typically use tools like axios or fetch.

load more v

This article will go over how to fetch data with Axios in React, save it to state, and then display it in a React component.,And that’s all. This article is the entire process to retrieve and display API data in a React app. I wrote it because I couldn’t find a good guide for this one operation online.,Now that axios is installed, we can use it to make a GET request to retrieve all of the notes in our database. We want to store this request inside a function.

Add `useEffect` to your React import, and simply call the function inside the useEffect hook:

useEffect(() => {
}, []);
load more v

You are fetching data correctly, but setting data to state wrongly.,Also when you iterating your data array, you are printing table head each time which is wrong and from your data array address and company are object so you cant direct print the object., Do satellites decrease the amount of solar radiation reaching Earth?

You need to do this,

const App = () => {
  const url = ''

  const [data, setData] = useState([])

  useEffect(() => {
    axios.get(url).then(json => setData(
  }, [])

  const renderTable = () => {
    return => {
      return (
          <td>{user.address.street}</td> //only street name shown, if you need to show complete address then you need to iterate over `user.address` object
          <td>{}</td> //only company name shown, if you need to show complete company name then you need to iterate over `` object

  return (
      <h1 id="title">API Table</h1>
      <table id="users"> //Your Table in post changed to table to make it work


3. Now create an asynchronous function with the name getUsersData and fetch data from JSON placeholder using axios. After the response, change the state of your users and loading using the setState method, and then call that function inside your componentDidMount lifecycle method.,4. Now create a variable with the name Column inside render function which will contain an array of objects.,For this tutorial, we will be using react-table@6.11.5 and data from JSON placeholder

  1. Open your project directory and install Axios and react-table.
npm install axios react - table @6 .11 .5--save

How to Make a GET Request (Retrieve Data),How to Set Up Axios with React,Why Use Axios in React?

If you have an existing React project, you just need to install Adios with npm (or any other package manager):

npm install axios
load more v

What axios enables us to do is to use the exact same promise syntax as fetch – but instead of using our first then callback to manually determine whether the response is okay and throw an error, axios takes care of that for us.,Additionally, it enables us in that first callback to get the JSON data from ,React Query not only allows us to use custom hooks that we can reuse across our components in a concise way, but it also gives us a great deal of state management tools to be able to control when, how, and how often our data is fetched.

In this example, we will simply revise our Fetch example by first installing axios using npm:

npm install axios

So which React data fetching method is better: fetch() or Axios? It depends on the particular requirements of your project and your comfort level when it comes to using built-in APIs.,React components can just fetch their own data. The big question is when to fetch the data. There are several options:,The fetchUsers() sets the isFetching state variable to true, so while fetching new data, the component renders the “Fetching users…” message. Then it gets the users by some “magic” and the sets isFetching back to false.

  • Start with no data, and fetch data based on user actions like clicking a button
  • Load the data once
  • Load the data periodically

Since the component is totally autonomous, no other component can tell it that it’s time to load its data. In this case, I chose to load the data for the first time in componentDidMount() and also set a timer that will fetch the data again every five seconds.

load more v

The method to fetch posts will look like the one used in the first part.,For Steps 3-5 to work, we need to make the request to fetch data from an API. This is where the JSONplaceholder API will come in handy for our example.,Like we did with the Fetch API, let’s start by requesting data from an API. For this one, we’ll fetch random users from the Random User API.

We will be fetching random users from JSONPlaceholder, a fake online REST API for testing. Let’s start by creating our component and declaring some default state.

class App extends React.Component {
  state = {
    isLoading: true,
    users: [],
    error: null

  render() {
load more v

Almost every web and mobile application retrieve the data when a user makes a request. Web applications get the data from the database when a user makes request.,Now, we will use the Axios with React app to make the Axios POST request and send the user data to the MongoDB server.,In this Axios with React tutorial with example, we will create a basic MERN app. This app will have users, and users can be created, retrieved, updated, and deleted from the MongoDB database using Axios.

npx create - react - app react - axios - tutorial

To run the React app, run the following command:

npm start

Run below command to install Bootstrap 4 UI library in React app.

npm install bootstrap--save

Add bootstrap.min.css file in src/App.js file:

import React from 'react';
import '../node_modules/bootstrap/dist/css/bootstrap.min.css';

function App() {
  return (
    <div className="App">
      <h2>React Axios Demo</h2>

export default App;
load more v

So this is how we can use Axios client to consume a REST API.,So that’s how we can use the Fetch API to consume a REST API. In the next section, we’ll be discussing Axios and how we can use it to consume the same API in the same App.,APIs are what we can use to supercharge our React applications with data. There are certain operations that can’t be done on the client-side, so these operations are implemented on the server-side. We can then use the APIs to consume the data on the client-side.

   "login": "hacktivist123",
   "id": 26572907,
   "node_id": "MDQ6VXNlcjI2NTcyOTA3",
   "avatar_url": "",
   "gravatar_id": "",
   "url": "",
   "html_url": "",
   "followers_url": "",
   "following_url": "{/other_user}",
   "gists_url": "{/gist_id}",
   "starred_url": "{/owner}{/repo}",
   "subscriptions_url": "",
   "organizations_url": "",
   "repos_url": "",
   "events_url": "{/privacy}",
   "received_events_url": "",
   "type": "User",
   "site_admin": false,
   "name": "Shedrack akintayo",
   "company": null,
   "blog": "",
   "location": "Lagos, Nigeria ",
   "email": null,
   "hireable": true,
   "bio": "☕ Software Engineer | | Developer Advocate🥑|| ❤ Everything JavaScript",
   "public_repos": 68,
   "public_gists": 1,
   "followers": 130,
   "following": 246,
   "created_at": "2017-03-21T12:55:48Z",
   "updated_at": "2020-05-11T13:02:57Z"
load more v

Other "display-table" queries related to "Using Axios to fetch data and display it in Table in React"