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 trial

JavaScript

Norman Lew
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Norman Lew
Full Stack JavaScript Techdegree Graduate 14,784 Points

Why are there brackets around this code?

Hi, could you explain why there are brackets around this code?

{this.state.players.map( (player, index) => <Player score={player.score} name={player.name} id={player.id} key={player.id.toString()} index={index} changeScore={this.handleScoreChange} removePlayer={this.handleRemovePlayer} /> )}

2 Answers

HIDAYATULLAH ARGHANDABI
HIDAYATULLAH ARGHANDABI
21,058 Points

Dear Norman, you have asked a good question in ReactJS when we write HTML and in the HTML div we want to manipulate a place we open two {} braces. Here you are reading the Player array from the state and sending it to the player component as props.

{
    this.state.players.map( (player, index) => 
     <Player 
          score={player.score} 
          name={player.name} 
          id={player.id} 
          key={player.id.toString()} 
          index={index} 
          changeScore={this.handleScoreChange} 
          removePlayer={this.handleRemovePlayer} 
       /> 
)}

It's hard to say without context, but it appears to be inside a react component, specifically within JSX.

If that's the case, it's inside curly braces so that when the JSX gets compiled, it knows to interpret that as javascript.