Skip to content

Issues with ES6 classes #2575

Closed
Closed
@ceane

Description

@ceane

I'm having issues with ES6 classes in React. It seems that esprima-harmony differs from how 6to5 is implementing classes, as well as how classes are implemented in beta versions of browsers.

With esprima-harmony I am able to use classes (using React.createClass(MyClass.prototype)), however,

If I use any of these methods:

with 6to5

React.createClass(MyComponent.prototype);

In Chrome 39 and IE11 Windows 10 Preview

React.createClass(MyComponent);

I get this error: Error: Invariant Violation: createClass(...): Class specification must implement arendermethod.. And if I try using React.createFactory in 6to5 I get this error: TypeError: undefined is not an object (evaluating 'nextElement.props').

I can't figure out what would be so different between Object.defineProperty and directly defining prototypes, other than the fact that properties set on the prototype aren't automatically enumerable.

ES6 code

class MyComponent {
  render() {
    return null;
  }
}

esprima-harmony compiles:

function MyComponent() {}
MyComponent.prototype.render = function() {return null;}

6to5 compiles:

var _classProps = function(child, staticProps, instanceProps) {
  if (staticProps) Object.defineProperties(child, staticProps);
  if (instanceProps) Object.defineProperties(child.prototype, instanceProps);
};

var MyComponent = (function() {
  var MyComponent = 
     function MyComponent() {};

  _classProps(MyComponent, null, {
    render: {
      writable: true,

      value: function() { 
        return null;
      }
    }
  });

  return MyComponent;
})();

How should I resolve this issue?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions