-
Notifications
You must be signed in to change notification settings - Fork 801
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
Invalid 'this' binding with async arrow function class properties #391
Comments
I am also getting this error, when I remove |
It should be a troubling bug to me, too. |
Is it possible to disable hot loader for component/method that fails? |
@valerymercury not currently. it might be worth adding an option to the Babel plugin to opt out of class properties transform. |
@Kovensky: I took a look at this, and I think it's two things: there's a problem with the RHL plugin, where we unnecessarily add |
So I was working on a potential fix on Weirdly enough, if I just run the plugin ahead of time without other Babel transforms and copy the output (using astexplorer), the code runs fine. But if I run it as a normal plugin, it has the broken Reproduce project here, I'm kind of stuck on this right now. Anyone else have any ideas? |
Oddly enough, removing the I no longer get the invalid |
@danielarias123 yeah, but we don't want people to have to remove |
@calesce hmmm that's weird because both stateless functions that return |
@danielarias123 Right, but they won't retain their state and will remount, you can check by adding |
* [xde] Build XDE with Webpack Build XDE with Webpack. Benefits: - Adds HMR capabilities - Tree shaking with web pack 2 - Potential other optimizations in the future that I haven't thought of yet. Key points: - Keeps node_modules in `app` unbundled -- at the moment, there's no benefit in bundling them. - Uses source maps to keep debug-ability (for some reason source-map support was turned off in Chrome Developer Tools in Electron for me...make sure to turn it on) - You can run with hot module reloading turned off or on -- run `npm run start[-local | -staging]-hot` to use it, omit the `-hot` to not. - Modified npm scripts so that you don't have to run any watcher script separately. Just run the correct `npm run start-blahblahblha` command and go. - No need to `yarn` in both directories -- `yarn` in `dev/xde` will use a postinstall script to `yarn` in the `dev/xde/app` directory and also rebuild any node_modules using electron rebuild. - When bundling with Webpack, don't transpile commonJS modules -- Webpack understands these and uses them to do tree shaking. - Temporarily, we need to use the `es2015` preset with babel when using HMR -- there is a bug: gaearon/react-hot-loader#391 cc @jesseruder @ide * [xde] Install React Developer Tools * [xde] Update yarn.lock. * [XDE] Fix native dependency installation, update Electron * [xde] Rebuild when app starts, not on install * [xde] Fix dirname issue in renderer * [xde] Clean up gulp + webpack config * Fix yarn * [xde] Fixup babel/HMR config * re yarn * s/index/renderer * separate entry point for HMR fbshipit-source-id: f4e811b
I've found that the order of the plugins in your
This also gives the error:
However, this works fine:
|
I managed to find the root cause of the problem as it appeared for us too. The problem is strictly connected to the copy of arrow function created as a method in a plugin. However, it's OK when there's no I assume there's a problem with babel itself rather than methods used there, but there's a part of code I found harmful for this case: react-hot-loader/src/babel/index.js Line 206 in 51dae3f
I also created an issue with more details on babel's repo: babel/babel#5078 And here is the reproduction repository: https://github.com/wkwiatek/babel-async-test BTW: @calesce, do you need some help on the v3 milestone? |
@wkwiatek is this fixed now? I got this error too. How can I workaround it? |
@mqklin same here, have you found a workaround? |
This doesn't work: class App extends React.Component {
myAsyncMethod = async () => {
return new Promise((resolve) => resolve(true))
}
render() {
return (
<Button onPress={this.myAsyncMethod} />
)
}
} However, this works fine with: class App extends React.Component {
async myAsyncMethod() {
return new Promise((resolve) => resolve(true))
}
render() {
return (
<Button onPress={() => this.myAsyncMethod()} />
)
}
} |
@andreatosatto90 no, I still use v2 :( I tried this #391 (comment) but it causes state reload - #642, so I'm just waiting for updates on this issue. |
I found another workaround: class App extends React.Component {
myAsyncMethod = () => async () => {
return new Promise((resolve) => resolve(true))
}
render() {
return (
<Button onPress={this.myAsyncMethod()} />
)
}
} |
This issue is not limited to async functions. #554 |
I am escaping this issue with: class App extends React.Component {
myAsyncMethod = async () => {
const _ = arguments // eslint-disable-line
return new Promise((resolve) => resolve(true))
}
render() {
return (
<Button onPress={this.myAsyncMethod} />
)
}
} or using autobind-decorator class App extends React.Component {
@autobind
async myAsyncMethod() {
return new Promise((resolve) => resolve(true))
}
render() {
return (
<Button onPress={this.myAsyncMethod} />
)
}
} |
+1 |
It seems that using the alternative config by removing react-hot-loader/babel from .babelrc and adding react-hot-loader/webpack to webpack.config.js works. It is not the same thing but it is good enough for me. Source: http://gaearon.github.io/react-hot-loader/getstarted/
|
.. that means that babel transformation, to support arrow functions can be removed!? Let me double check. It should not work. |
FYI (Blindly) downgrading from 3.0.0+ to 3.0.0-beta.7 worked for me. |
some problem when upgrade react-hot-loader, in async arrow function, 'this' is undefined. |
This is known bug. A solution was found and will come in next version. |
Can confirm also worked for me. This issue in v3 (also along with #650) is super frustrating, good to hear next version will be fixing things but right now am stuck on an old version with all warnings suppressed and HMR basically not working properly -- on an otherwise very vanilla create-react-app babel config. |
I found that running a The problem seems to happen when it interacts with other transforms; something is executing out of order and changing things before I then tried printing the node that |
This would be definitely fixed in v4 since we do not transpile arrow functions any more. |
Confirm, it's fixed in v4 |
I found a workaround that doesn't make you to change any code except the method declaration. Just wrap it in IIFE: class App extends React.Component {
myAsyncMethod = (() => async () => {
return new Promise((resolve) => resolve(true))
})()
render() {
return (
<Button onPress={this.myAsyncMethod()} />
)
}
} |
* [xde] Build XDE with Webpack Build XDE with Webpack. Benefits: - Adds HMR capabilities - Tree shaking with web pack 2 - Potential other optimizations in the future that I haven't thought of yet. Key points: - Keeps node_modules in `app` unbundled -- at the moment, there's no benefit in bundling them. - Uses source maps to keep debug-ability (for some reason source-map support was turned off in Chrome Developer Tools in Electron for me...make sure to turn it on) - You can run with hot module reloading turned off or on -- run `npm run start[-local | -staging]-hot` to use it, omit the `-hot` to not. - Modified npm scripts so that you don't have to run any watcher script separately. Just run the correct `npm run start-blahblahblha` command and go. - No need to `yarn` in both directories -- `yarn` in `dev/xde` will use a postinstall script to `yarn` in the `dev/xde/app` directory and also rebuild any node_modules using electron rebuild. - When bundling with Webpack, don't transpile commonJS modules -- Webpack understands these and uses them to do tree shaking. - Temporarily, we need to use the `es2015` preset with babel when using HMR -- there is a bug: gaearon/react-hot-loader#391 cc @jesseruder @ide * [xde] Install React Developer Tools * [xde] Update yarn.lock. * [XDE] Fix native dependency installation, update Electron * [xde] Rebuild when app starts, not on install * [xde] Fix dirname issue in renderer * [xde] Clean up gulp + webpack config * Fix yarn * [xde] Fixup babel/HMR config * re yarn * s/index/renderer * separate entry point for HMR
Async arrow functions use an invalid 'this' binding when they appear as class properties, as long as the
react-hot-loader/babel
plugin is present at all.Arrow functions transformed by react-hot-loader also seem to not respect the
spec
setting given to thetransform-es2015-arrow-functions
plugin; however settingspec
tofalse
does not help avoid the invalid code either.I've added some snippets of the problematic code, and also some comments annotating both the input and the output.
Snippets of the generated code; in the constructor:
In the
_createClass
helper's argument list:The text was updated successfully, but these errors were encountered: