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
Riks Fontein
7,523 PointsOne thing I don't understand, can somebody explain this?
I get the whole onClick method mine is the same: onClick={() => changeScore(index, -1)}
What I don't get about this is the anonymous function we pass to the method, can someone explain this to me?
Riks Fontein
7,523 PointsHere is the link to the video, https://teamtreehouse.com/library/update-state-based-on-a-players-index
1 Answer
Jesse Jafa
33 PointsIn the Player.js file the <Counter /> component is passed the index as prop, so it has access to it. If you resume the video at 1:48, you'll be able that now each player has their own unique index. It's like having an array of players, and using player[1], player[1] etc.
Previously you used
props.changeScore(-1)
which just changed the score without looking for a specific player
By adding the index like this
changeScore(index, -1)}
the function is now looking to change the score of the player who has the index that was passed on. Basically, by passing the index to the changeScore function, now all the +/- buttons correspond to their players.
Michael Hulet
47,913 PointsMichael Hulet
47,913 PointsIt doesn't look like your question is linked to any particular video/quiz/challenge. Could you please link it so we have some of the context you're referring to?