weixin_33743703 2016-04-20 16:39 采纳率: 0%
浏览 45

React JS Ajax数据循环

I am very new to React (Building my first application today). I am using an ajax plugin (react-ajax) and so far so good on single JSON calls to an API. However I am struggling to learn how to loop through entires to output multiple results.

    var playersUrl = "/public/index.php/players.json";
    var PlayerList = React.createClass({
    getInitialState: function() {
        return {
            players: ''
        };
    },
    responseHandler: function(err, data){
        this.setState({
            players: data.body.data
        })
    },
    render: function() {
        return (
            React.createElement("ul", {className: "players"},
                React.createElement(Ajax, {url: playersUrl, onResponse: this.responseHandler}),
                this.state.players.map(function (player) {
                  return <li>player.id</li>
                })
            )
        )
    }
});
ReactDOM.render(<PlayerList />, document.getElementById('player-list'));

Basically looking to write an each loop to output some Html populated with the JSON results from the response handler. Thanks a bunch!

{
   "data":[
      {
         "id":"18",
         "firstName":"Graham",
         "gender":{
            "label":"Male",
            "value":"male",
            "selected":true
         },
         "preferred_position":{
            "label":"Attacking",
            "value":"attacking",
            "selected":true
         },
         "dob":{
            "date":"1991-03-25 00:00:00.000000",
            "timezone_type":3,
            "timezone":"UTC"
         },
         "PassingRatingAvg":2.5,
         "ShootingRatingAvg":5,
         "DribblingRatingAvg":3,
         "PaceRatingAvg":2,
         "DefendingRatingAvg":1,
         "OverallRating":13.5,
         "TotalRating":13
      }
   ],
   "meta":{
      "pagination":{
         "total":1,
         "count":1,
         "per_page":100,
         "current_page":1,
         "total_pages":1
      }
   }
}

Edit: Current error "PlayerList.js:18 Uncaught TypeError: this.state.players.map is not a function".

  • 写回答

2条回答 默认 最新

  • MAO-EYE 2016-04-20 17:02
    关注

    If I understand your question correctly, your response body may contain multiple records about players. But your responseHandler only handles the the JSON format of one record. Can you describe the structure of your JSON response body? Basically, what you can do is add a players field to your state for multiple records about players, and replace your <h1>{this.state.name}</h1> with something like

    this.state.players.map(function (player) {
      return <h1>player.name</h1>
    })
    

    And responsehandler should be something like:

    function(err, data) {
        this.setState({players: data.body.data})
    }
    

    If you are not using es6, you can change that to:

    var playersDiv = []
    this.state.players.forEach(function(player) {
        playerDiv.push(<h1>player.name</h1>)
    })
    return playersDiv 
    
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog