JavaScript React Basics Understanding State Bind Event Handlers to Components

yoav green
yoav green
8,611 Points

render() binding to the class

so why from the first place i could have used "this" in the render() method? in a way it's also a custom method within the class.

Alexey Kulikov
Alexey Kulikov
11,123 Points

Hi!

I guess that is because React calls render() method on your component (instance of Counter class which extends React.Component) e.g. counterObj.render(). So React takes care of the right context of "this" inside render(). But onClick handler is called simply as a function (not as a method on counterObj). You can even pass a function that is defined outside of your component's class, to onClick="...".