当前位置: 首页 > 面试题库 >

React.js通过数组创建循环

阴凯歌
2023-03-14
问题内容

我正在尝试显示10位玩家的表格。我通过ajax获取数据,并将其作为道具传递给我的孩子。

var CurrentGame = React.createClass({

  // get game info
  loadGameData: function() {
    $.ajax({
      url: '/example.json',
      dataType: 'json',
      success: function(data) {
        this.setState({data: data});
      }.bind(this),
      error: function(xhr, status, err) {
        console.error('#GET Error', status, err.toString());
      }.bind(this)
    });
  },

  getInitialState: function(){
    return {data: []};
  },

  componentDidMount: function() {
    this.loadGameData();
  },

  render: function() {
    return (
      <div className="CurrentGame">
        <h1> Current Game Information</h1>
        <PlayerList data={this.state.data}/>
      </div>
    );
  }
});

现在,我需要一个列表组件来渲染播放器:

var PlayerList = React.createClass({


  render: function() {

    // This prints the correct data
    console.log(this.props.data);

    return (
      <ul className="PlayerList">
        // I'm the Player List {this.props.data}
        // <Player author="The Mini John" />

        {
          this.props.data.participants.map(function(player) {
            return <li key={player}>{player}</li>
          })
        }
      </ul>
    )
  }
});

这给了我一个Uncaught TypeError: Cannot read property 'map' of undefined

我不确定发生了什么,我的控制台日志显示了正确的数据,但是以某种方式我无法在返回中访问它。

我想念什么?


问题答案:

CurrentGame组件中,您需要更改初始状态,因为您正在尝试使用for,participants但是此属性是undefined导致错误的原因。

getInitialState: function(){
    return {
       data: {
          participants: [] 
       }
    };
},

此外,如player.mapObject你应该从它那里得到的属性

this.props.data.participants.map(function(player) {
   return <li key={player.championId}>{player.summonerName}</li>
   // -------------------^^^^^^^^^^^---------^^^^^^^^^^^^^^
})

Example



 类似资料:
  • 本文向大家介绍php compact 通过变量创建数组,包括了php compact 通过变量创建数组的使用技巧和注意事项,需要的朋友参考一下 php compact 通过变量创建数组 compact函数通过一个或多个变量建立一个数组,改数组包括变量名和它们的值。 该函数的行为与extract()正好相反。 基本语法 array compact ( mixed $varname [, mixed

  • 问题内容: 在python中使用MySQLdb库时,如何定义多语句函数或过程? 例: 这将创建以下回溯: 如果我将相同的SQL直接复制到mysql shell客户端中,它将按预期工作 问题答案: 该命令是内置的MySQL Shell客户端,只有该程序(和MySQL查询浏览器)才能识别。如果您直接通过API执行SQL语句,则无需使用。 的目的是帮助您避免在语句本身可以包含分号字符时避免终止语句。这在

  • 问题内容: 我有选择标签的 数组 。 我想在JavaScript中创建一个具有两个字段’uniqueIDofSelect和optionValue’的json对象。 我使用getElementsByName(“ status”)并对其进行迭代。 编辑 我需要像 等等… 问题答案: 据我对您的要求的理解,这应该有效:

  • 问题内容: 我想使用simpleJdbcInsert类和executeBatch方法 http://static.springsource.org/spring/docs/2.5.x/api/org/springframework/jdbc/core/simple/SimpleJdbcInsert.html 所以我需要传递一个as参数数组。如何创建这样的数组?我试过的是 错误:无法创建通用数组 A

  • 问题内容: 我正在编写一个应用程序,以便在对等网络中进行一些分布式计算。在定义网络时,我有两个类P2PNetwork和P2PClient。我希望这些是通用的,因此具有以下定义: 与P2PClient一起定义setNetwork(T network)的方法。我希望用这段代码描述的是: P2PNetwork由某种类型的客户端组成 P2PClient只能属于其客户端与该客户端具有相同类型的网络(循环引用

  • 问题内容: 嗨,我正在尝试创建一组六边形集(六边形是我创建的类)。 但是当我尝试编译时出现此错误 我该如何解决? 问题答案: 您不能使用泛型创建数组。使用或代替。 这是正式的解释。