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 trialGonzalo Muro
5,281 PointsI dont understand yet what is the .bind(this) function for. Can you explain that to me please?
In the "Handling Events" section of the React Documentation, says the following:
// This binding is necessary to make this
work in the callback
this.handleClick = this.handleClick.bind(this);
, but why?
Is it because if we don't do that "this" will access only to his own scope (handleClick function) instead of the component scope?
rahulsao
6,752 Pointsrahulsao
6,752 Points"this" inside a function refers to an object, but which object it refers to depends on the how the function is called.
Consider the following example from "YDKJS"
You use bind() to create bounded methods and by binding this.handleClick to "this" , you make sure that no matter how you call your function the context remains the same.