0

This is a simple question. How to display the data in the table from json file?

table.js

   componentDidMount() {
    
        fetch('/apps.json')
       .then(rsp => rsp.json())
        .then(data => data.map(item => {
            return console.log(item);
        }));
    }

      <tbody> 
     {/* { data.map(item => {
      return(
         <tr>
        <td>{item.id}</td>
        <td>{item.name}</td>
        <td>{item.dateCreated}</td>
       </tr>
      );
      })} */}
     </tbody>
   

3 Answers 3

1
// 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

Sign up to request clarification or add additional context in comments.

3 Comments

@WMax Yes, it is.
@WMax I have added the code for class component too.
Ah okay. Now I understand a bit about the use of state/hooks. Thanks again for the info!
0

You can work with react-data-table-component its very simple to use this is a simple :

    import React from 'react';
    import DataTable from 'react-data-table-component';
    
    
  
    const data= [{"id":1,"name":"Alphazap","url":"http://1und1.de","devOpsUrl":"https://webs.com/nunc/commodo/placerat/praesent/blandit/nam/nulla.jsp","techStack":"sit amet cursus id turpis integer aliquet massa id lobortis convallis tortor risus dapibus","dateCreated":"05.06.2020"}]

      const columns = [
      // your columns here 
      ];
    
      return (
        <>

          <DataTable
            title="my table"
            columns={columns}
            data={data}
          />
        </>
      );
    };

Check here for more infos and here for more simples

1 Comment

Thanks for the info! But I want to try to create this table without using external packages.
0

After you have converted the response received from the server into JSON, you can then set the JSON formatted response in your state variable using the setState() method.

componentDidMount() {
    fetch("/apps.json")
    .then((rsp) => rsp.json())
    .then((rsp) => {
      this.setState({
        appsData: rsp
      });
    });
}

In your JSX code, you can uncomment the code you have written with a small change that you need to add a unique key prop to each item in your list. This helps React identify which elements have really changed so as to render them again.

{ data.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>
    );
})}

Read more:

React Documentation: setState()

React Documentation: Lists and Keys

1 Comment

Thank you for the info! I managed to get it

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.