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 trialTyler McDonald
Full Stack JavaScript Techdegree Graduate 16,700 PointsWhy must we use an arrow function for onClick?
Why can't we just do this: onClick={ props.changeScore(-1) }
instead of this onClick={ () => props.changeScore(-1) }
1 Answer
Steven Parker
231,269 PointsIf you define a function, it will be performed later when the event occurs. Otherwise, the code will be performed now and the results will be assigned where a function is expected; and the error will not be detected until the event occurs.
Tyler McDonald
Full Stack JavaScript Techdegree Graduate 16,700 PointsTyler McDonald
Full Stack JavaScript Techdegree Graduate 16,700 PointsThat makes sense. So would it be similar to trying something like this?
document.querySelector('#element').addEventListener(myFunction(1))
Steven Parker
231,269 PointsSteven Parker
231,269 PointsThat code would call "myFunction" now instead of passing a reference to it for the listener to call later.