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

One 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?

It 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?

1 Answer

In 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.