4

Using ES6+ syntax in React/React-Native, the variable foo, when defined outside of the constructor is somehow transformed into an instance variable when called with this.. Is my assertion true? Why does it even work, when not instantiated in the constructor? Here a corresponding React Native code snippet:

class myComponent extends Component {
  constructor() {
    super();
  }

  foo = "bar";

  render() {
    return ( <View>{ this.foo }</View> );
  }
}

This discussion about ES7 property initializers shows how the state variable is prominently used in this way in React/React Native.

So far related Stack Overflow discussions I read through here and here could not answer this question for me..

Community
  • 1
  • 1
Andru
  • 5,954
  • 3
  • 39
  • 56
  • 2
    "*Why does it even work, when not instantiated in the constructor?*" - because class fields are a horrible idea – Bergi Mar 05 '16 at 23:05
  • Related post [here](https://stackoverflow.com/q/37788342/465053). – RBT Oct 02 '17 at 01:18

1 Answers1

4

Your assertion is true.

The problem is that it's currently just in the stage-1, so it's not clear when and if ever it becomes the standard.

References:

zerkms
  • 249,484
  • 69
  • 436
  • 539
  • I see. Thanks for the links! I wasn't aware about the entire ECMAScript development process with different stages and such. – Andru Mar 05 '16 at 22:08