// Set appsData state
componentDidMount() {
fetch("/apps.json")
.then((rsp) => rsp.json())
.then((rsp) => {
this.setState({
appsData: rsp
});
});
// Map the data
{ this.state.appsData.map(item => {
return(
<tr key={item.id}>
<td>{item.id}</td>
<td>{item.name}</td>
<td>{item.url}</td>
<td>{item.devOpsUrl}</td>
<td>{item.techStack}</td>
<td>{item.dateCreated}</td>
</tr>
);
})}
If you're interested in hooks, you can use the useState and useEffect hooks.
import React, { useState, useEffect } from "react";
const Apps = () => {
const [data, setData] = useState([])
useEffect(() => {
fetch("/apps.json")
.then((rsp) => rsp.json())
.then((data) => setData(data))
}, [])
return (
<div>
<h4>Apps List</h4>
<table border={1} cellPadding={5}>
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>URL</th>
<th>DevOps URL</th>
<th>Tech Stack</th>
<th>Date Created</th>
</tr>
</thead>
<tbody>
{ data.map(item => (
<tr key={item.id}>
<td>{item.id}</td>
<td>{item.name}</td>
<td>{item.url}</td>
<td>{item.devOpsUrl}</td>
<td>{item.techStack}</td>
<td>{item.dateCreated}</td>
</tr>
))}
</tbody>
</table>
</div>
);
}
export default Apps
References:
Using the State Hook
Using the Effect Hook