Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Context in Facebook's React JS framework

I'm struggling to understand how context works in Facebook's React JS framework.

When passing a specification to React.createClass, certain methods (notably event handlers) seem to require the use of React.autoBind to 'bind callbacks to the component'. Other methods (notably render()) don't have this requirement but still happily reference this.props or this.state.

What is the context of 'this' as used by the render() method, if it's not the component ?

like image 395
Justin Avatar asked Oct 22 '22 07:10

Justin


1 Answers

That's because they kind of already does autoBind for internal methods such as render. In fact, if you call autoBind and pass those methods you'll get an error.

For custom methods, initially the idea is that you might want to stick to whatever context you want to assign, but this is changed in 0.4.x (http://facebook.github.io/react/blog/2013/07/02/react-v0-4-autobind-by-default.html).

Basically because the bound this is most of the time what you want, from now on every method in createClass will now autoBind by default.

like image 191
chenglou Avatar answered Oct 24 '22 03:10

chenglou