Skip to content
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

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

Closed
Brian-Kaplan opened this issue Oct 5, 2017 · 14 comments
Labels
Stale There has been a lack of activity on this issue and it may be closed soon.

Comments

@Brian-Kaplan
Copy link

Brian-Kaplan commented Oct 5, 2017

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

@Brian-Kaplan Brian-Kaplan changed the title [0.49.1] Statically defined variables in a component do not persist [0.49.1] Statically defined variables in a component do not persist after mounting Oct 5, 2017
@nicholasmarais1158
Copy link

A more simple example to demonstrate this issue. Appears to be an issue when using class variables in jsx/tsx.

import React, {
  Component,
} from 'react';
import {
  View,
} from 'react-native';

class TestComponent extends Component<{}, {}> {
  static SIZE = 100;

  public render(): JSX.Element {
    return <View
      style={{
        width: TestComponent.SIZE,
        height: TestComponent.SIZE,
        backgroundColor: 'red'
      }}/>
  }
}

export default TestComponent;

@Brian-Kaplan
Copy link
Author

TestComponent.SIZE is null when given to the style?

@nicholasmarais1158
Copy link

undefined to be specific.

10-16 15:15:51.747 7406-9094/com.example I/ReactNativeJS: TestComponent.SIZE = undefined

import React, {
  Component,
} from 'react';
import {
  View,
} from 'react-native';

class TestComponent extends Component<{}, {}> {
  static SIZE = 100;

  public render(): JSX.Element {
    console.log(`TestComponent.SIZE = ${TestComponent.SIZE}`);

    return <View
      style={{
        width: TestComponent.SIZE,
        height: TestComponent.SIZE,
        backgroundColor: 'red'
      }}/>
  }
}

export default TestComponent;

@nicholasmarais1158
Copy link

I'm using the following workaround until this issue is explained and/or fixed;

import React, {
  Component,
} from 'react';
import {
  View,
} from 'react-native';

const SIZE = 100; // Solution if this constant is used outside of this component

class TestComponent extends Component<{}, {}> {
  private readonly SIZE_INTERNAL = 100; // Solution if this constant is used only within this component

  public render(): JSX.Element {
    console.log(`this.SIZE_INTERNAL = ${this.SIZE_INTERNAL}`);
    console.log(`SIZE = ${SIZE}`);

    return <View
      style={{
        width: this.SIZE_INTERNAL,
        height: this.SIZE_INTERNAL,
        backgroundColor: 'red'
      }}/>
  }
}

export default TestComponent;
export {
  SIZE as SIZE_EXTERNAL,
}

10-17 08:09:52.931 4104-4427/com.example I/ReactNativeJS: this.SIZE_INTERNAL = 100
10-17 08:09:52.943 4104-4427/com.example I/ReactNativeJS: SIZE = 100

@stale
Copy link

stale bot commented Dec 16, 2017

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Maybe the issue has been fixed in a recent release, or perhaps it is not affecting a lot of people. If you think this issue should definitely remain open, please let us know why. Thank you for your contributions.

@stale stale bot added the Stale There has been a lack of activity on this issue and it may be closed soon. label Dec 16, 2017
@stale stale bot closed this as completed Dec 23, 2017
@aarondail
Copy link

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

@hramos hramos reopened this Jan 24, 2018
@stale stale bot removed the Stale There has been a lack of activity on this issue and it may be closed soon. label Jan 24, 2018
@hramos
Copy link
Contributor

hramos commented Jan 24, 2018

I suspect this is more of an issue in React. I'm reopening, though I think you might have better luck checking out https://reactjs.org/community/support.html for pointers.

@vTrip
Copy link

vTrip commented Jan 29, 2018

Also getting undefined when accessing statics on a component - please keep this issue open

@stale
Copy link

stale bot commented May 16, 2018

Hey there, it looks like there has been no activity on this issue recently. Has the issue been fixed, or does it still require the community's attention? This issue may be closed if no further activity occurs. You may also label this issue as "For Discussion" or "Good first issue" and I will leave it open. Thank you for your contributions.

@stale stale bot added the Stale There has been a lack of activity on this issue and it may be closed soon. label May 16, 2018
@react-native-bot
Copy link
Collaborator

It looks like your issue may refer to an older version of React Native. Can you reproduce the issue on the latest release, v0.55?

@stale stale bot removed the Stale There has been a lack of activity on this issue and it may be closed soon. label May 16, 2018
@darksheepgod
Copy link

darksheepgod commented Jul 12, 2018

Still happens on v0.55.4.
Should be a Metro bug.

It only occurs when using static properties. For now just use static methods instead.
e.g. change "SIZE" to getSIZE() and setSIZE()

@phips28
Copy link

phips28 commented Aug 3, 2018

still happens on v0.56.

@stale
Copy link

stale bot commented Nov 1, 2018

Hey there, it looks like there has been no activity on this issue recently. Has the issue been fixed, or does it still require the community's attention? This issue may be closed if no further activity occurs. You may also label this issue as "For Discussion" or "Good first issue" and I will leave it open. Thank you for your contributions.

@stale stale bot added the Stale There has been a lack of activity on this issue and it may be closed soon. label Nov 1, 2018
@stale
Copy link

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.

@stale stale bot closed this as completed Nov 8, 2018
@facebook facebook locked as resolved and limited conversation to collaborators Nov 9, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Stale There has been a lack of activity on this issue and it may be closed soon.
Projects
None yet
Development

No branches or pull requests

8 participants