-
Notifications
You must be signed in to change notification settings - Fork 24.4k
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
Class instance properties do not work in async arrow functions #24068
Comments
I believe this is actually an issue with console.log (i had similar weird behaviour logging around async code with a non react js function earlier today) Can you test by calling an alert instead of using console.log? Unable to repro when assigning to new value and displaying (number should be a value assignment) https://snack.expo.io/@jkcooper/cm4yND |
@JKCooper2, thanks for the reply! It worked on the expo link, but using my Android Emulator it didn't. "Mount" and "Other" were empty on screen. Here are my dependencies and devDependencies from package.json. It's a brand new project from "react-native init".
|
I've been unable to reproduce on a fresh project with the following: export default class App extends Component {
constructor() {
super();
//Instance property
this.number = 1;
}
//async arrow function
componentDidMount = async () => {
console.warn('mount', this.number);
console.log('mount', this.number);
this.otherMethod();
};
//"normal" async function
async otherMethod() {
console.warn('otherMethod', this.number);
console.log('otherMethod', this.number);
}
render() {
return (
<View style={styles.container}>
<Text style={styles.welcome}>Welcome to React Native!</Text>
<Text style={styles.instructions}>To get started, edit App.js</Text>
</View>
);
}
} I've tried this on device, and whilst debugging: Debugging: @rrgomide please can you create a reproducible example that we could clone Thanks |
@Salakar I think you nailed it along with @JKCooper2! I did the same as @Salakar did and it seems the problem only happens while debugging the code. I've been using VSCode with "React Native Tools" Extension to do so. So I guess the problem is not related to react-native. Thank you so much for your time! |
🐛 Bug Report
Hi, I've been trying to upgrade a project created with React Native 0.51.0 and, starting from 0.57, class instance properties are not working correctly anymore in async arrow functions (in my opinion). They're correctly interpreted in async functions (not arrow functions) as you'll see in the code examples. In my old project everything works as expected. My guess is that it's a problem with babel config, since it has been upgraded to version 7 and at react-native 0.51 it was version 6.
To Reproduce
Create any project from react-native 0.57 or later versions. I haven't tried with Expo.
Expected Behavior
this.number
should be read correctly on componentDidMount arrow function.Code Example
I put the following similar code in Babel's "try out" feature and tested on Chrome. Everything worked as expected. That's why I created the issue here, on react-native project:
Environment
React Native Environment Info:
System:
OS: Windows 7
CPU: (8) x64 Intel(R) Core(TM) i7-4770 CPU @ 3.40GHz
Memory: 173.64 MB / 7.91 GB
Binaries:
Node: 10.15.1 - C:\nodejs\node.EXE
Yarn: 1.13.0 - C:\Program Files (x86)\Yarn\bin\yarn.CMD
npm: 6.7.0 - C:\nodejs\npm.CMD
The text was updated successfully, but these errors were encountered: