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 React Components (2018) Managing State and Data Flow Update State Based on a Player's Index

accessing "score" directly from state as a member of Player allowed?

in 2:32 in function handleScoreChange(), is it allowed to access and change the score inside the players array this way, how come? score is not an item in the state but a member in the players array, so my confusion is from this part. Is there any additional reading about this?

Peter Mingione
Peter Mingione
10,484 Points

I have the same question. How does the setState method in the handleScoreChange method know which score to change in the player's array?

handleScoreChange = (index, delta) => {
    this.setState( prevState => ({
      score: prevState.players[index].score += delta
    }));
    // console.log('index: ' + index, 'delta: ' + delta);
  } = (index, delta) => {
    this.setState( prevState => ({
      score: prevState.players[index].score += delta
    }));
    // console.log('index: ' + index, 'delta: ' + delta);
  }