Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React.js create loop through Array

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 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>     );   } }); 

Now I need a List Component to Render the Players:

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>     )   } }); 

Which gives me a Uncaught TypeError: Cannot read property 'map' of undefined.

I'm kind of unsure what is happening, my console log displays the correct data but somehow I can't access it in the return.

What am I missing ?

like image 351
Mini John Avatar asked Feb 04 '15 11:02

Mini John


People also ask

How do you loop over an object in React?

Use the Object. keys() method to get an array of the object's keys. Use the map() method to iterate over the array of keys.

How do you get data from array of objects in React JS?

To render an array of objects in React, we can use the array map method. const Item = (props) => { return <li>{props. message}</li>; }; const TodoList = () => { const todos = ["foo", "bar", "baz"]; return ( <ul> {todos. map((message) => ( <Item key={message} message={message} /> ))} </ul> ); };

Can you use forEach in React?

Using the forEach() method in React # The forEach() method can be used to iterate over an array outside of your JSX code in React. If you need to iterate over an array and render its elements directly in your JSX code, use the map() method instead.


1 Answers

In CurrentGame component you need to change initial state because you are trying use loop for participants but this property is undefined that's why you get error.,

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

also, as player in .map is Object you should get properties from it

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

Example

like image 141
Oleksandr T. Avatar answered Sep 24 '22 01:09

Oleksandr T.