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 Building Applications with React and Redux Putting it all Together Updating the Player, Counter and AddPlayerForm Components

At 2.06 why did we update to onClick={() => props.removePlayer(props.index)}?

At 2.06 why did we update to onClick={() => props.removePlayer(props.index)}? and not just onClick={props.removePlayer(props.index)}

1 Answer

You have to use an anonymous arrow function to pass an argument to the click handler function, otherwise, you'd actually be calling the function when the code was executed.

props.removePlayer(props.index) is actually a function call.

() => props.removePlayer(props.index) is an anonymous function that returns a function.

So the rule of thumb, whenever you need to pass an argument to your handler, wrap it in an arrow function.