loop in react jsx

PHOTO EMBED

Thu Aug 27 2020 20:08:59 GMT+0000 (Coordinated Universal Time)

Saved by @vrzi #javascript #react.js

render() {
  const elements = ['one', 'two', 'three'];

  const items = []

  for (const [index, value] of elements.entries()) {
    items.push(<li key={index}>{value}</li>)
  }

  return (
    <div>
      {items}
    </div>
  )
}

////////// or:

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