pablocattaneo
12/27/2018 - 11:03 AM

How to iterate an array in react?

How to iterate an array in react? #react

import React from 'react'

const cpTable = (props) => {

  let trThead = null
  let trTbody = null

  if (props.data.rowContent){
    trThead = (
        props.data.rowContent.map( rowContent => {
          return (
            <tr>
              {
                Object.keys(rowContent).map(key => {
                  return <th>{rowContent[key]}</th>
                })
              }
            </tr>
          )
        })
    )
    trTbody = (
      <tr>
        <td>The table body</td>
      </tr>
    )
  }

  return (
    <div className="table-responsive">
      <h1>{props.data.title}</h1>  
      <table className="table table-striped table-bordered">
        <thead>
          {trThead}
        </thead>
        <tbody>
          {trTbody}
        </tbody>
      </table>
    </div>
  )
}

export default cpTable