Back when React 0.13-beta was released I refactored a large project from using React.createClass to ES6 classes. There was only one difference between the two. React.createClass binds “this” to methods by default but classes does not.
There are multiple solutions to this problem (pun not intended):
- Revert back to React.createClass - no practical cons, but you are relying on a magic that createClass does and don’t use the standard class.
- Use .bind on methods in the constructor or in the render - a lot of manual boilerplate. Binding in the constructor does not work with React Hot Loader and binding in the render creates a new function on every render.
Rethinking best practices… again
It is quite easy to write a Babel plugin that autobinds class methods, you can check my implementation here: https://github.com/UgnisSoftware/babel-plugin-autobind-class-methods.