Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialelisha day
5,115 PointsI followed everything in this video and it is saying cannot read property 'map' of undefined
here is my code
const App = (props) => { return ( <div className="scoreboard"> <Header title="Scoreboard" totalPlayers={4} />
{/* Players list */}
{props.initialPlayers.map(player =>
<Player
name={props.name}
score={props.score}
/>
)}
</div>
); }
ReactDOM.render( <App initialPlayers={ players}/>, document.getElementById('root') );
Dane Cook
Front End Web Development Techdegree Graduate 15,464 PointsHello, Try changing props.name to player.name and props.score to player.score so that it will be referencing each object being iterated on.
elisha day
5,115 PointsI dont know where to put the player.forEach here is my new code...
const App = (props) => { return ( <div className="scoreboard"> <Header title="Scoreboard" totalPlayers={4} />
{/* Players list */}
{this.props.initialPlayers.map(player =>
<Player
name={player.name}
score={player.score}
/>
)}
</div>
); }
2 Answers
Hakan Ergoksen
2,168 PointsThis is how your code should look like:
const players = [
{
name: "Elisha",
score: 10
},
{
name: "Hakan",
score: 20
},
{
name: "Dane",
score: 30
},
{
name: "James",
score: 40
},
];
const Header = (props) => {
return (
<header>
<h1>{ props.title }</h1>
<span className="stats">Players: { props.totalPlayers }</span>
</header>
);
};
const Player = (props) => {
return (
<div className="player">
<span className="player-name">
{props.name}
</span>
<Counter score={props.score} />
</div>
);
};
const Counter = (props) => {
return (
<div className="counter">
<button className="counter-action decrement"> - </button>
<span className="counter-score">{ props.score }</span>
<button className="counter-action increment"> + </button>
</div>
);
};
const App = (props) => {
return (
<div className="scoreboard">
<Header
title="Scoreboard"
totalPlayers={props.initialPlayers.length}
/>
{/* Players list */}
{props.initialPlayers.map( player =>
<Player
name= {player.name}
score={player.score}
/>
)}
</div>
);
};
ReactDOM.render(
<App initialPlayers={players} />,
document.getElementById('root')
);
Dane Cook
Front End Web Development Techdegree Graduate 15,464 Pointstry
(props.initialPlayers.map(player =>
instead of
(this.props.initialPlayers.map(player =>
elisha day
5,115 PointsI DID TRY THAT AND IM STILL GETTING MAP UNDEFINED ERROR
Hakan Ergoksen
2,168 PointsHakan Ergoksen
2,168 PointsYou need to add 'player' value in the '<Player />' tag's attribute. Because it is an iteration.
Example:
If it is a javascript code block. It looks like this:
You need to change your <Player /> tag like below: