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 trialOvidiu Cotorogea
12,998 PointsWhy access the function like that?
onClick={() => props.removePlayer(props.id)}
And why can't you access it like this ?
onClick= {props.removePlayer(props.id)}
1 Answer
Ben Wilson
4,323 PointsBy 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
12,998 PointsUhhh makes so much sense, thank you!
Peter Retvari
Full Stack JavaScript Techdegree Student 8,392 PointsPeter Retvari
Full Stack JavaScript Techdegree Student 8,392 PointsI'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