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".