javascript - In React, how to show data from API in a HTML table? - Stack Overflow

I'm trying to display data from my example API. At the moment I'm able to loop through the ar

I'm trying to display data from my example API. At the moment I'm able to loop through the array, and display all the titles, for example:

EventTitle1
EventTitle2
EventTitle3
...

Here is my code:

class App extends React.Component {
constructor() {
   super();
   this.state = {
      data: []
   }
}
 
ponentDidMount() {
 var th = this;
 this.serverRequest = axios.get(this.props.source)
 .then(function(event) { 
     th.setState({
         data: event.data
     });
 })
}

ponentWillUnmount() {
  this.serverRequest.abort();
}
 
render() {
 var titles = []
 this.state.data.forEach(item => {
    titles.push(<h3 className=”events”>{item.title[0].value}</h3> );
 })
 return (
    <div className=”container”>
      <div className=”row”>
         <div className=”col-md-6 col-md-offset-5">
             <h1 className=”title”>All Events</h1>
             {titles} 
         </div>
      </div>
    </div>
  );
 } 
}

ReactDOM.render(
   <App source=”http://localhost:8888/example/api/events" />, 
   document.getElementById(‘container’)
);

I'm trying to display data from my example API. At the moment I'm able to loop through the array, and display all the titles, for example:

EventTitle1
EventTitle2
EventTitle3
...

Here is my code:

class App extends React.Component {
constructor() {
   super();
   this.state = {
      data: []
   }
}
 
ponentDidMount() {
 var th = this;
 this.serverRequest = axios.get(this.props.source)
 .then(function(event) { 
     th.setState({
         data: event.data
     });
 })
}

ponentWillUnmount() {
  this.serverRequest.abort();
}
 
render() {
 var titles = []
 this.state.data.forEach(item => {
    titles.push(<h3 className=”events”>{item.title[0].value}</h3> );
 })
 return (
    <div className=”container”>
      <div className=”row”>
         <div className=”col-md-6 col-md-offset-5">
             <h1 className=”title”>All Events</h1>
             {titles} 
         </div>
      </div>
    </div>
  );
 } 
}

ReactDOM.render(
   <App source=”http://localhost:8888/example/api/events" />, 
   document.getElementById(‘container’)
);

How could I create an HTML table, to display only the five latest event titles from the API (and later other data) inside the table?

For example:

return (
    <table>
      <tr>
        <th>Event title</th>
        <th>Event location</th> 
      </tr>
      <tr>
        <td>First event title {titles[0]} ?? </td> 
        <td>San Fran</td>
      </tr>
      <tr>
        <td>Second event title {titles[1]} ??</td> 
        <td>London</td> 
      </tr>
    </table>
);
Share Improve this question asked Oct 25, 2016 at 8:15 userdenuserden 1,6937 gold badges27 silver badges54 bronze badges 2
  • Event location is not from the API? – Pranesh Ravi Commented Oct 25, 2016 at 8:19
  • Yes, that also es from the API – userden Commented Oct 25, 2016 at 8:19
Add a ment  | 

3 Answers 3

Reset to default 2

Create an array of <tr> and add it into the table.

class App extends React.Component {
constructor() {
   super();
   this.state = {
      data: []
   }
}
 
ponentDidMount() {
 var th = this;
 this.serverRequest = axios.get(this.props.source)
 .then(function(event) { 
     th.setState({
         data: event.data
     });
 })
}

ponentWillUnmount() {
  this.serverRequest.abort();
}
 
render() {
 const contents = this.state.data.forEach(item => {
      // change the title and location key based on your API
      return <tr>
        <td>{item.title}</td> 
        <td>{item.location}</td>
      </tr>
 })
 return (
    <div className="container">
      <div className="row">
         <div className="col-md-6 col-md-offset-5">
             <h1 className="title">All Events</h1>
             <table>
              <tr>
                <th>Event title</th>
                <th>Event location</th> 
              </tr>
                {contents}
            </table>
         </div>
      </div>
    </div>
  );
 } 
}

ReactDOM.render(
   <App source="http://localhost:8888/example/api/events" />, 
   document.getElementById("container")
);

You can simply use the slice operator to get the first 5 elements of you array, and then directly use map.

There is also no need to use a titles array, you can just put the snippet inline in the jsx.

The important part of the code is:

<table>
    {data.slice(0,5).map(event => (<tr>
        <td>{event.title}</td>
        <td>{event.location}</td>
    </tr>))}
</table>

Here is the full example:

class App extends React.Component {
constructor() {
   super();
   this.state = {
      data: []
   }
}
 
ponentDidMount() {
 var th = this;
 this.serverRequest = axios.get(this.props.source)
 .then(function(event) { 
     th.setState({
         data: event.data
     });
 })
}

ponentWillUnmount() {
  this.serverRequest.abort();
}
 
render() {
 return (
    <div className=”container”>
      <div className=”row”>
         <div className=”col-md-6 col-md-offset-5">
            <h1 className=”title”>All Events</h1>
            <table>
               <tr>
                  <th>Event title</th>
                  <th>Event location</th> 
               </tr>
               {this.state.data.slice(0,5).map(event => (<tr>
                  <td>{event.title}</td>
                  <td>{event.location}</td>
               </tr>))}
            </table>
         </div>
      </div>
    </div>
  );
 } 
}

ReactDOM.render(
   <App source=”http://localhost:8888/example/api/events" />, 
   document.getElementById(‘container’)
);

I just would not code it in the above examples like that.

  1. I would not use setting variables to this reminds me the old days of angularjs which a lot of people did var vm = this (vm for view model )
  2. Need to be using modern javascript with arrow functions and use map and not forEach

    sample below:

    class App extends React.Component {
    
        constructor(props) {
            super(props);
            this.state = {
               data: []
            }
        }
    
        ponentDidMount() {
    
           axios.get(this.props.source)
               .then((event) => { 
                   this.setState({
                       data: event.data
                   });
           })
        }
    
    
    render() {
         const yourData = this.state.data.map(item => (
         // notice array so no return here , ( ) instead of { } 
        // notice also map instead of forEach creates a closure - map is preferred
        <tr>
            <td>{item.title}</td> 
            <td>{item.location}</td>
        </tr>
    })
    return (
       <div className=”container”>
            <div className=”row”>
                 <div className=”col-md-6 col-md-offset-5">
                     <h1 className=”title”>All Events</h1>
                      <table>
                        <tr>
                           <th>Event title</th>
                            <th>Event location</th> 
                        </tr>
                       {yourData}
                   </table>
                 </div>
              </div>
          </div>
         );
       } 
    }
    
     ReactDOM.render(
       <App source=”http://localhost:8888/example/api/events" />, 
           document.getElementById(‘container’)
     );
    
     // above obviously works and old way of doing things, and that is for a div id of containter
    // I would setup an api to append to and use the ponentDidMount() {... }  
    // then -->   export default App; 
    

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742225442a4404618.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信