Building a Dynamic Table in React JS using Data from an API

Posted by


In this tutorial, we will learn how to create a dynamic table in React JS and populate it with data fetched from an API.

We will be using the fetch API to make a GET request to fetch data from a sample API.

Here are the steps to create a dynamic table with data fetched from an API in React JS:

Step 1: Create a new React project
First, create a new React project by running the following command in your terminal:

npx create-react-app dynamic-table

This command will create a new React project called dynamic-table.

Step 2: Create a Table component
Next, create a new file called Table.js in the src/components folder. This file will contain our Table component.

In Table.js, add the following code:

import React from 'react';

const Table = ({ data }) => {
  return (
    <table>
      <thead>
        <tr>
          <th>Name</th>
          <th>Email</th>
          <th>Phone</th>
        </tr>
      </thead>
      <tbody>
        {data.map((item) => (
          <tr key={item.id}>
            <td>{item.name}</td>
            <td>{item.email}</td>
            <td>{item.phone}</td>
          </tr>
        ))}
      </tbody>
    </table>
  );
};

export default Table;

Step 3: Fetch data from the API
In the src folder, create a new file called App.js.

In App.js, add the following code:

import React, { useState, useEffect } from 'react';
import Table from './components/Table';

const App = () => {
  const [data, setData] = useState([]);

  useEffect(() => {
    fetch('https://jsonplaceholder.typicode.com/users')
      .then((response) => response.json())
      .then((json) => setData(json));
  }, []);

  return <Table data={data} />;
};

export default App;

In this code, we are using the useState and useEffect hooks to fetch data from the API and store it in the data state variable.

Step 4: Run the React app
To run the React app, navigate to the project folder in your terminal and run the following command:

npm start

This will start the development server and open the app in your default browser. You should see a dynamic table populated with data fetched from the API.

That’s it! You have successfully created a dynamic table in React JS and populated it with data fetched from an API. Feel free to customize the table component and fetch data from a different API if needed.

I hope this tutorial was helpful! Let me know if you have any questions.

0 0 votes
Article Rating

Leave a Reply

22 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
@ThapaTechnical
2 hours ago

👉Free Source Code Link: https://www.thapatechnical.com/2023/02/creating-dynamic-table-with-data.html

😊 Become Member, get access to perks, free Source code, & more..

https://www.youtube.com/channel/UCwfaAHy4zQUb2APNOGXUCCA/join

😍 Check my Instagram to Connect with me: https://www.instagram.com/thapatechnical

👩‍💻 Discord Server Link for Programmer to Hangout: https://discord.gg/MdScmCsua6

@devl0ver666
2 hours ago

i love your videos

@rasidpathan9896
2 hours ago

Worst

@NavyasriThalluri
2 hours ago

Add english subtitles

@mrunknown_811
2 hours ago

thank you

@pravindhodare2813
2 hours ago

The content is excellent, but your voice is too intensive, please try to use better and mice for recording.

@pankajkori2462
2 hours ago

Respect++ sir🙏🙏

@Kavitaehyung_
2 hours ago

Please Sir make a video for fetching data from Spotify api. Please Please I'm stucked here🙏😢

@chandrakantojha5290
2 hours ago

Boom guys, and we learned how to populate table using react😄

@muhammadzuhairaskari7924
2 hours ago

🎉❤thnks sirr

@archanakumari286
2 hours ago

Thank you so much , it was very helpful☺

@HarshSingh-kr2ci
2 hours ago

[00:07]Creating a dynamic table with data fetched from an API in React

[02:03] Creating a dynamic table with React JS

[03:58] Creating a dynamic table with data fetched from an API

[06:17] Creating a dynamic table with fetched data

[08:19] Using React JS to create a dynamic table with data fetched from an API.

[10:38] Creating a dynamic table with data fetched from an API

[12:47] Creating a dynamic table with data fetched from an API

[14:54] Fetching and displaying API data in a dynamic table

[17:05] Learn about React JS and API data fetchin

@vatslalit1408
2 hours ago

dear thapa ji faltu boom guys mat kiya kro or mudde ki baat kiya kro

@anshulagarwal568
2 hours ago

Bhai detail me kuch nhi samjhata .. kewal rattu gyan bata deta hai 😅

@iceheart_601
2 hours ago

thanks bro….

@shreyash9357
2 hours ago

Please make video on send table row data as array of object api call

@rahulsagar1992
2 hours ago

Booom 🔥🔥🔥

@shivajijadhav2864
2 hours ago

Murga kab katnewale ho…shor machara hai…🐔

@ektabansal1829
2 hours ago

Thanks alot Thapa Technical. I'm really grateful for you.

@Dev-Phantom
2 hours ago

cool

22
0
Would love your thoughts, please comment.x
()
x