Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

makeAssimilatePrototype invokes getters when copying methods #131

Closed
timnew opened this issue May 14, 2015 · 3 comments
Closed

makeAssimilatePrototype invokes getters when copying methods #131

timnew opened this issue May 14, 2015 · 3 comments
Milestone

Comments

@timnew
Copy link

timnew commented May 14, 2015

I have React component defined in es6 style class, within it, I declared some getters.
But the getters got executed in makeAssimilatePrototype, which is improper behavior!

class PropertyBinder extends React.Component {
  constructor(props) {
    super(props);

    this.state = new Map({
      childProps: {},
      listeners: {}
    });
    this.onSourceChanged();
  }

  get multiSource() { return this.props.multiSource; }
  get childProps() { return this.state.childProps; }
  get listeners() { return this.state.listeners; }

  // some other code
}

I got following call stack

Uncaught TypeError: Cannot read property 'multiSource' of undefined
  _createClass.get  @   PropertyBinder.jsx:53
  (anonymous function)  @   makeAssimilatePrototype.js:44
  updateStoredPrototype @   makeAssimilatePrototype.js:43
  assimilatePrototype   @   makeAssimilatePrototype.js:62
  patchReactClass   @   makePatchReactClass.js:40
  makeHot   @   makeMakeHot.js:33
  makeExportsHot    @   makeExportsHot.js:17
  (anonymous function)  @   PropertyBinder.jsx:128
  (anonymous function)  @   PropertyBinder.jsx:128
  (anonymous function)  @   PropertyBinder.jsx:129
  (anonymous function)  @   bundle.js:685
  __webpack_require__   @   bundle.js:505
  fn    @   bundle.js:76
  (anonymous function)  @   Setup.jsx:25
  (anonymous function)  @   Setup.jsx:120
  (anonymous function)  @   Setup.jsx:121
  (anonymous function)  @   bundle.js:595
  __webpack_require__   @   bundle.js:505
  fn    @   bundle.js:76
  (anonymous function)  @   route.jsx:19
  (anonymous function)  @   route.jsx:42
  (anonymous function)  @   route.jsx:43
  (anonymous function)  @   bundle.js:553
  __webpack_require__   @   bundle.js:505
  fn    @   bundle.js:76
  (anonymous function)  @   multi_main:3
  (anonymous function)  @   bundle.js:535
  __webpack_require__   @   bundle.js:505
  (anonymous function)  @   bundle.js:528
  (anonymous function)  @   bundle.js:531
@timnew
Copy link
Author

timnew commented May 14, 2015

To fix this issue, I think it should avoid to read and set property directly, instead call Object.getOwnPropertyDescriptor() to read property and call Object.defineProperty again to override it. This approach should work because descriptor.configurable is set to true by default;

this is what _createClass look like:

"use strict";

var _Object$defineProperty = __webpack_require__(29)["default"];

exports["default"] = (function () {
  function defineProperties(target, props) {
    for (var i = 0; i < props.length; i++) {
      var descriptor = props[i];
      descriptor.enumerable = descriptor.enumerable || false;
      descriptor.configurable = true;
      if ("value" in descriptor) descriptor.writable = true;

      _Object$defineProperty(target, descriptor.key, descriptor);
    }
  }

  return function (Constructor, protoProps, staticProps) {
    if (protoProps) defineProperties(Constructor.prototype, protoProps);
    if (staticProps) defineProperties(Constructor, staticProps);
    return Constructor;
  };
})();

exports.__esModule = true;

/*****************
 ** WEBPACK FOOTER
 ** ./~/babel-runtime/helpers/create-class.js
 ** module id = 23
 ** module chunks = 0
 **/

@gaearon
Copy link
Owner

gaearon commented Aug 22, 2015

This is fixed in the next beta release coming very soon.
Stay tuned.

@gaearon gaearon closed this as completed Aug 22, 2015
@gaearon gaearon added this to the v2.0 milestone Aug 22, 2015
@gaearon
Copy link
Owner

gaearon commented Aug 23, 2015

Can I ask you to check whether react-hot-loader@2.0.0-alpha solves this issue?
Please provide any feedback on it in this PR: #182

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants