Skip to content

Improper 'this' context during createClass(): 0.11.1 #2183

@lwhorton

Description

@lwhorton

In React 0.10, I am able to decorate the render method with a static render that returns a promise, as opposed to React's own callback implementation.

mixinRender = function(spec) {
    var statics
    spec.statics = statics = (spec.statics || {})
    // spot weld a static 'render' method with a .then()
    statics.render = statics.render || staticRenderFactory.call(this,spec)
    return spec
}

function staticRenderFactory(spec) {
    return function staticRender(model, node) {
        var promisified = Promise.promisify(React.renderComponent, React)

        var node = (node || spec.node(model))
        var Ctor = this(model) // <- this during runtime refers to the createClass() function
        return promisified(Ctor, node)
    }
}

This semi-decoration worked well enough for my purposes. It returns to the caller a descriptor as I would expect.

createClass: function(spec) {
    var Constructor = function() {};
    Constructor.prototype = new ReactCompositeComponentBase();
    Constructor.prototype.constructor = Constructor;

    var DescriptorConstructor = Constructor;

    var ConvenienceConstructor = function(props, children) {
      var descriptor = new DescriptorConstructor();
      descriptor.construct.apply(descriptor, arguments);
      return descriptor; // <- tada!
    };

However, following the same pattern in 0.11.1 gives me a no property 'construct error during the internal createClass() call.

Digging around in the codebase, it just looks like a forgotten context bind.

  createClass: function(spec) {
    var Constructor = function(props, owner) {
      this.construct(props, owner); // <- this is undefined, and hence construct is undefined
    };
    Constructor.prototype = new ReactCompositeComponentBase();
    Constructor.prototype.constructor = Constructor;

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