question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[0.49.1] Statically defined variables in a component do not persist after mounting

See original GitHub issue

Is 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

https://snack.expo.io/Sy6djeN3W

image

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:9
  • Comments:14 (1 by maintainers)

github_iconTop GitHub Comments

5reactions
aarondailcommented, Jan 4, 2018

Could we have this reopened, it is an actual problem with a simple repro?

0reactions
stale[bot]commented, Nov 8, 2018

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Where are static variables stored in C and C++?
Global/Static Global or static variables and objects have their storage allocated at program startup, but may not be initialized until after the program...
Read more >
How to Use Variables within Classes | Pluralsight
In this guide, we will learn how to declare and work with various types of variables within classes, including the this keyword, static,...
Read more >
Dynamic behavior in Svelte: working with variables and props
Our Todos.svelte component is currently just displaying static markup; let's start making it a bit more dynamic.
Read more >
Programming FAQ — Python 3.11.1 documentation
Why am I getting an UnboundLocalError when the variable has a value? What are the rules for local and global variables in Python?...
Read more >
5.1.9.3 Persisted System Variables - MySQL :: Developer Zone
The MySQL server maintains system variables that configure its operation. A system variable can have a global value that affects server operation as...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found