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 trialMaya O'Reilly
11,141 PointsIn setRating = { () => this.handleSetRating(i + 1) }; Does handleRating(i + 1) using closure underneath?
Is handleRating(i + 1) using closure underneath to remember the i(th) value?
1 Answer
Brandon White
Full Stack JavaScript Techdegree Graduate 35,771 PointsHi Maya O'Reilly,
I may not be understanding your question correctly (so let me know if Iβm off base with the following response), but the i(th) value is determined by which iteration of the for...loop the Star component is created in.
Essentially, when the Star component is created and pushed into the array, the argument is passed into the function at that time. So i + 1 during the first loop passes 1 into the this.handleSetRating function. During the second loop i + 1 evaluates to 2 (as i is now 1), and so 2 is passed in as an argument. During the third loop i + 1 is 3, and so on and so forth. None of these functions are invoked unless itβs corresponding Star component is clicked, but closures are not needed in this situation.