Created
January 2, 2015 20:50
-
-
Save paulirwin/bae92250cb8196bb2d77 to your computer and use it in GitHub Desktop.
Use plain ES6 classes in JSX with React
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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