温馨提示:本站仅提供公开网络链接索引服务,不存储、不篡改任何第三方内容,所有内容版权归原作者所有
AI智能索引来源:http://www.muo.com/react-generate-table-from-json/
点击访问原文链接

How to Generate a Table From JSON Data in React

How to Generate a Table From JSON Data in React Menu Sign in now Close Trending Submenu Productivity Android Smart TVs Networking Windows 11 Entertainment PC & Mobile Submenu Windows2 Linux Android Apple Technical Submenu Tech Explained Security Software Submenu Productivity Internet Creative Screen Submenu Entertainment Streaming Home Submenu Smart Home Home News Sign in Newsletter Menu Follow Followed Like More Action Sign in now Productivity Android Smart TVs Networking Windows 11 Entertainment Close
How to Generate a Table From JSON Data in React Image from Pexels Follow Followed Like Link copied to clipboard Add us on By  Mary Gathoni Published Aug 18, 2022, 8:00 AM EDT Mary writes for the programming section and has been doing so for the past two years. Her educational background is in Computer Science and Physics. Sign in to your MakeUseOf account

One of the most straightforward ways to separate data from your HTML documents is to store it in JSON. JSON is popular and easy to work with, especially in JavaScript.

In React, it makes sense to serve JSON data via tables using a component. That component will be able to generate a table that scales with the JSON data. The resulting table can have as many rows as it needs since the data is not hard-coded.

What You Will Need

You will need Node.js installed on your machine to follow this tutorial and a basic understanding of how React works.

Before creating the table, you will need to create a new React project if you don’t have one.

Creating the JSON Data

The table will use data stored in a JSON file. You might fetch this data from an API endpoint in a real-life application.

In the src folder, create a new file called data.json and add the following:

[{
"id": 1,
"first_name": "Ethelred",
"last_name": "Slowly",
"email": "eslowly0@google.es"
},{
"id": 2,
"first_name": "Reta",
"last_name": "Woolmer",
"email": "rwoolmer1@miibeian.gov.cn"
},{
"id": 3,
"first_name": "Arabel",
"last_name": "Pestor",
"email": "apestor2@bloglovin.com"
}]

This is a simple JSON file containing three objects.

The object keys—the id, first name, last name, and email—are the headings, while their corresponding properties make up the table’s body.

Creating the Table Component

Create a new file called Table.js in the src folder and add the following code.

export default function Table({theadData, tbodyData}) {
return (



// header row



// body data


);
}

This component takes theadData and tBodyData as props. theadData contains the data that you’ll display in the header row. The app will source this data from the JSON file and hand it over to the Table component.

Create a function in App.js called getHeadings() and add the following.

const getHeadings = () => {
return Object.keys(data[0]);
}

Since the keys for each object in the JSON file are similar, you can simply use the keys from the first object.

Remember to import data.json in App.js.

import data from "./data.json"

When you render the Table component, pass the heading and the JSON data as props.

 
Creating the Header Row

In this step, you will create a component to render an item in the header row. This component will iterate over the headings using the map() method.

In Table.js, add the code to iterate over the headings inside the thead tag.

{theadData.map(heading => {
return {heading}
})}

Next, you will populate the body of the table.

Creating the Body Rows

The table body renders the row data. Since Table.js receives the data as an array of objects, you will need to iterate over it first to get each object representing a row.

So, in Table.js, iterate over the tBodyData prop like this:

{tbodyData.map((row, index) => {
return
// row data
;
})}

Each row object will be similar to the object example below.

const row = {
"id": 1,
"first_name": "Ethelred",
"last_name": "Slowly",
"email": "eslowly0@google.es"
}

To display each of these items, you will need to iterate over the keys of the object. In each iteration, you’ll retrieve the property that matches that key in the row object. Since these keys are the same as the headings, use the values from the theadData prop.

Modify the tr tag to display the row data as shown below.

// theadData contains the keys
{theadData.map((key, index) => {
return {row[key]}
})}
;

Bringing everything together, the Table component should look like this:

export default function Table({theadData, tbodyData}) {
return (



{theadData.map(heading => {
return {heading}
})}



{tbodyData.map((row, index) => {
return
{theadData.map((key, index) => {
return {row[key]}
})}
;
})}


);
}

In the element, the component iterates over the data array and returns the table row for each object.

Using the Table Component

Import Table in App.js and render it as shown below:

import Table from './Table';
import data from "./data.json"

function App() {
const getHeadings = () => {
return Object.keys(data[0]);
}

return (



);
}
export default App;

The table component takes theadData and tbodyData as props. theadData contains the headings generated from the keys of the first item in the JSON data, and tbodyData contains the whole JSON file.

Styling With CSS Modules

You generated a React table component from a JSON file in this tutorial. You also learned how you can manipulate JSON data to fit your needs. You can improve the looks of your table by adding some CSS to it. To create locally scoped CSS styles, consider using CSS modules. It is simple to use and easy to get started with if you are using a create-react-app application.

Close
Recommended How to Style React Components Using CSS Modules The best sci-fi show of the past decade isn't on Netflix, HBO, or Disney+ The new Boox Go 10.3 is an eReader I genuinely don't want to put down I tried these 6 new Excel functions and they saved me a ton of time Join Our Team Our Audience About Us Press & Events Media Coverage Contact Us Follow Us Advertising Careers Terms Privacy Policies MakeUseOf is part of the Valnet Publishing Group Copyright © 2026 Valnet Inc.

How to Generate a Table From JSON Data in React,AI智能索引,全网链接索引,智能导航,网页索引

    Learn how a React component can generate markup and access data in the JSON format.