Skip to content

Instantly share code, notes, and snippets.

@paulirwin
Created January 2, 2015 20:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save paulirwin/bae92250cb8196bb2d77 to your computer and use it in GitHub Desktop.
Save paulirwin/bae92250cb8196bb2d77 to your computer and use it in GitHub Desktop.
Use plain ES6 classes in JSX with React
// This gets around the stock .hasOwnProperty check in React 0.12 that prevents using plain ES6 classes
// as a parameter to React.createClass or React.createFactory. Or, at least, this makes it work with
// the React.NET transformer -- I cannot testify that this is needed for traditional JSX transformation.
class ES6Reactifier {
static createFactory(type) {
var obj = {};
for (var m in type.prototype) {
obj[m] = type.prototype[m];
}
return React.createFactory(React.createClass(obj));
}
}
// Usage:
class MyComponent {
render() {
return <h1>Hello, ES6 classes with React!</h1>;
}
}
var MyComponentFactory = ES6Reactifier.createFactory(MyComponent);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment