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

Ovidiu Cotorogea
Ovidiu Cotorogea
12,998 Points

Why access the function like that?

onClick={() => props.removePlayer(props.id)}

And why can't you access it like this ?

onClick= {props.removePlayer(props.id)}

Peter Retvari
seal-mask
.a{fill-rule:evenodd;}techdegree
Peter Retvari
Full Stack JavaScript Techdegree Student 8,392 Points

I'm not sure, just guessing but maybe for the onClick event we need a callback function, which means that one the user clicks on the button it will trigger this removeplayer method

1 Answer

Ben Wilson
Ben Wilson
4,323 Points

By adding the parenthesis at the end, you will invoke the function immediately. But wrapping this in an anonymous function, () => you can prevent this behaviour until the onClick handler is triggered.

Note that if you do not need to pass a property to your function you CAN use onClick={props.removePlayer}

But onClick={props.removePlayer()} will trigger when the component is rendered.

Hope that helps!

Ovidiu Cotorogea
Ovidiu Cotorogea
12,998 Points

Uhhh makes so much sense, thank you!