loop in react code example

Example 1: react for loop in render

render: function() {
  const elements = ['one', 'two', 'three'];
  return (
    <ul>
      {elements.map((value, index) => {
        return <li key={index}>{value}</li>
      })}
    </ul>
  )
}

Example 2: react for loop

<tbody>
  {[...Array(10)].map((x, i) =>
    <ObjectRow key={i} />
  )}
</tbody>

Example 3: react render for loop

// for loop workaround in react render
[...Array(n)].map((_, index) => <span key={index}>element {index}</span>)

Example 4: react loop through array

this.items = this.state.cart.map((item, key) =>    <li key={item.id}>{item.name}</li>);