React.js create loop through Array

后端 未结 3 1300
没有蜡笔的小新
没有蜡笔的小新 2020-12-08 02:36

I\'m trying to display a Table of 10 Players. I get the data from via ajax and pass it as props to my Child.

var CurrentGame = React.createClass({

  // get          


        
3条回答
  •  离开以前
    2020-12-08 03:09

    You can simply do conditional check before doing map like

    {Array.isArray(this.props.data.participants) && this.props.data.participants.map(function(player) {
       return 
  • {player.summonerName}
  • }) }

    Now a days .map can be done in two different ways but still the conditional check is required like

    .map with return

    {Array.isArray(this.props.data.participants) && this.props.data.participants.map(player => {
       return 
  • {player.summonerName}
  • }) }

    .map without return

    {Array.isArray(this.props.data.participants) && this.props.data.participants.map(player => (
       return 
  • {player.summonerName}
  • )) }

    both the above functionalities does the same

提交回复
热议问题