[0.49.1] Statically defined variables in a component do not persist after mounting
See original GitHub issueIs this a bug report?
Yes
Have you read the Contributing Guidelines?
Yes
Environment
Environment: OS: macOS Sierra 10.12.6 Node: 7.7.1 Yarn: Not Found npm: 5.0.3 Watchman: 4.7.0 Xcode: Xcode 9.0 Build version 9A235 Android Studio: Not Found
Packages: (wanted => installed) react: 16.0.0-beta.5 => 16.0.0-beta.5 react-native: 0.49.1 => 0.49.1
Steps to Reproduce
I create a component called StaticComponent that declares a static class variable called instance
to be null. Once this component is mounted it will set the static class variable instance
to be itself.
class StaticComponent extends Component {
static instance = null;
constructor(props) {
super(props);
}
componentDidMount() {
StaticComponent.instance = this;
}
...
Then any other component that renders this component should be able to get the exact instance that was created upon mounting. What is returned every time is null…
export default class App extends Component {
onPress() {
// instance is always null
const instance = StaticComponent.instance;
}
render() {
return (
<View style={styles.container}>
<Button onPress={this.onPress} />
<StaticComponent />
</View>
);
}
}
I can confirm that this worked in 0.48.2. After upgrading to 0.49.1 it fails. I can recreate the issue using a fresh project from of react-native init.
If you actually go to the snack below you’ll see that the instance is defined as it should be. Snack is still on react-native 0.48.0 so that’s why. This blog post talks about the versions they’re on.
"I am happy to announce… Expo SDK 21.0.0! It is based on React Native 0.48 “August”. https://blog.expo.io/expo-sdk-21-0-0-is-now-available-be33b79921b7
If I copy paste the code I put into the snack back into my editor then run it with 0.49.1, the StaticComponent is not defined when it should be.
Reproducible Demo
Issue Analytics
- State:
- Created 6 years ago
- Reactions:9
- Comments:14 (1 by maintainers)
Top GitHub Comments
Could we have this reopened, it is an actual problem with a simple repro?
Closing this issue after a prolonged period of inactivity. If this issue is still present in the latest release, please feel free to create a new issue with up-to-date information.