Note: This post was published at a time when React did not support ES6 (v12).
I have an ES6 class:
class BaseClass { getInitialState(){ return {message: 'Hello!'}; } render() { return ( <div> <div>{this.state.message}</div> </div> ) } }
What can I export to ES6 using this expression (source: respond to ES6 )
export default React.createClass(BaseClass.prototype)
It works great. Now I would like to use ES6 inheritance to extend the BaseClass class:
class ExtendedClass extends BaseClass{ getInitialState(){ return {message: "Hello! I'm an extension"}; } }
But when I call React.createClass in the ExtendedClass class, I got the following exception:
Invariant Violation: ReactCompositeComponentInterface: You are attempting to define `constructor` on your component more than once. This conflict may be due to a mixin.
I know that React 0.13 should be more ES6 friendly, but are there any ways to handle this?
EDIT:
I use Traceur to compile my ES6 classes. The output for ExtendedClass as follows:
function ExtendedClass() { "use strict"; if (BaseClass !== null) { BaseClass.apply(this, arguments); } } for (BaseClass____Key in BaseClass) { if (BaseClass.hasOwnProperty(BaseClass____Key)) { ExtendedClass[BaseClass____Key] = BaseClass[BaseClass____Key]; } } ____SuperProtoOfBaseClass = BaseClass === null ? null : BaseClass.prototype; ExtendedClass.prototype = Object.create(____SuperProtoOfBaseClass); ExtendedClass.prototype.constructor = ExtendedClass; ExtendedClass.__superConstructor__ = BaseClass; ExtendedClass.prototype.getInitialState = function() { "use strict"; return {message: "Hello! I'm an extension"}; }; React.createClass(ExtendedClass.prototype);
javascript ecmascript-6 reactjs traceur
Jbe
source share