developerg1000
developerg1000

Reputation: 121

loop through db results react

So I have a table, like so:

render() {
    return (
      <table id="customers">
  {/* <% if(data.length){ 
    for(var i = 0;i < data.length;i++) { %> */}
      <tr>
        <th>Name</th>
        <th>Email</th>
        <th>Message</th>
      </tr>
      <tr>
        <td>Alfreds Futterkiste</td>
        <td>Maria Anders</td>
        <td>Germany</td>
      </tr>
    </table>
    );
  }

I am able to pass the data through to the front end, however, I want to be able to loop through there data in the table. I know this is possible in ejs, but react is totally diff.

thanks 4 the help :)

Upvotes: 0

Views: 31

Answers (1)

Dr. Selva Mary G
Dr. Selva Mary G

Reputation: 688

You can try this.

   render() {
        return (
          <table id="customers">
     ​<tr>
           ​<th>Name</th>
           ​<th>Email</th>
           ​<th>Message</th>
         ​</tr>
      {data.length? data.map((item, key)=>{
                return <tr>
            <td>{item.name}</td>
            <td>{item.email}</td>
            <td>{item.message}</td>
          </tr>
            }):""}
    
        </table>
        );
      }

Upvotes: 1

Related Questions