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

Joe Anderson
Joe Anderson
1,442 Points

How does the setState function know we are adding the score to a specific player when it's just updating "score"?

I'm trying to figure out the logic behind our handler in the App.js file:

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

How does the app know which user is being updated when the setState is targeting "score"? I would normally think we would have to use:

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

But we don't don't need to do this and I'm wondering why and how?