-
-
Notifications
You must be signed in to change notification settings - Fork 5.7k
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
this
is not found error (undefined) when i write codes like doSomething = async () => this.xxx ...
#4550
Comments
You should avoid class Test {
doSomething = async function() {
console.log('Test: %O', this);
}
} |
Looks like a bug with With: 'use strict';
var _regenerator = require('babel-runtime/regenerator');
var _regenerator2 = _interopRequireDefault(_regenerator);
var _asyncToGenerator2 = require('babel-runtime/helpers/asyncToGenerator');
var _asyncToGenerator3 = _interopRequireDefault(_asyncToGenerator2);
var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck');
var _classCallCheck3 = _interopRequireDefault(_classCallCheck2);
var _createClass2 = require('babel-runtime/helpers/createClass');
var _createClass3 = _interopRequireDefault(_createClass2);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
var Test = function () {
function Test() {
var _this = this,
_arguments = arguments;
(0, _classCallCheck3.default)(this, Test);
this.doSomething = function () {
var _ref = (0, _asyncToGenerator3.default)(_regenerator2.default.mark(function _callee() {
var _args = _arguments;
return _regenerator2.default.wrap(function _callee$(_context) {
while (1) {
switch (_context.prev = _context.next) {
case 0:
_context.next = 2;
return _this.__doSomething__REACT_HOT_LOADER__.apply(_this, _args);
case 2:
return _context.abrupt('return', _context.sent);
case 3:
case 'end':
return _context.stop();
}
}
}, _callee, _this);
}));
return function NAME(_x) {
return _ref.apply(this, arguments);
};
}();
}
(0, _createClass3.default)(Test, [{
key: '__doSomething__REACT_HOT_LOADER__',
value: function () {
var _ref2 = (0, _asyncToGenerator3.default)(_regenerator2.default.mark(function _callee2() {
var _args2 = arguments;
return _regenerator2.default.wrap(function _callee2$(_context2) {
while (1) {
switch (_context2.prev = _context2.next) {
case 0:
_context2.next = 2;
return _this2.__doSomething__REACT_HOT_LOADER__.apply(_this2, _args2);
case 2:
return _context2.abrupt('return', _context2.sent);
case 3:
case 'end':
return _context2.stop();
}
}
}, _callee2, this);
}));
function __doSomething__REACT_HOT_LOADER__(_x2) {
return _ref2.apply(this, arguments);
}
return __doSomething__REACT_HOT_LOADER__;
}()
}, {
key: '__doSomething__REACT_HOT_LOADER__',
value: function () {
var _ref3 = (0, _asyncToGenerator3.default)(_regenerator2.default.mark(function _callee3() {
return _regenerator2.default.wrap(function _callee3$(_context3) {
while (1) {
switch (_context3.prev = _context3.next) {
case 0:
console.log('Test: %O', _this3);
case 1:
case 'end':
return _context3.stop();
}
}
}, _callee3, this);
}));
function __doSomething__REACT_HOT_LOADER__() {
return _ref3.apply(this, arguments);
}
return __doSomething__REACT_HOT_LOADER__;
}()
}]);
return Test;
}();
new Test().doSomething();
;
var _temp = function () {
if (typeof __REACT_HOT_LOADER__ === 'undefined') {
return;
}
__REACT_HOT_LOADER__.register(Test, 'Test', 'src/index.js');
}();
; Without: 'use strict';
var _regenerator = require('babel-runtime/regenerator');
var _regenerator2 = _interopRequireDefault(_regenerator);
var _asyncToGenerator2 = require('babel-runtime/helpers/asyncToGenerator');
var _asyncToGenerator3 = _interopRequireDefault(_asyncToGenerator2);
var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck');
var _classCallCheck3 = _interopRequireDefault(_classCallCheck2);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
var Test = function Test() {
var _this = this;
(0, _classCallCheck3.default)(this, Test);
this.doSomething = (0, _asyncToGenerator3.default)(_regenerator2.default.mark(function _callee() {
return _regenerator2.default.wrap(function _callee$(_context) {
while (1) {
switch (_context.prev = _context.next) {
case 0:
console.log('Test: %O', _this);
case 1:
case 'end':
return _context.stop();
}
}
}, _callee, _this);
}));
};
new Test().doSomething(); |
@danharper Thank you! |
track in gaearon/react-hot-loader/issues/391 |
@wuchu as a workaround for now, you can return a wrapped async function, a la: someAsyncFunction = () => (async () => {
console.log('*this* is correct ->', this);
})(); |
@leebenson In order to preserve the 'this' point, maybe it is the best solution for me from then now, although codes look like a little strange. Thank you very much. : ] 👍 |
closing as it seems like a bug in gaearon/react-hot-loader? |
Input Code
Babel Configuration (.bablerc, package.json, cli command)
.bablerc
package.json
Expected Behavior
build ok!
Current Behavior
index.js:8307 Uncaught (in promise) ReferenceError: _this3 is not defined
Possible Solution
Context
i dont want via this style to write code like above.
Your Environment
The text was updated successfully, but these errors were encountered: