vjd
vjd

Reputation: 101

Declaring global class variables in React Native

In my class constructor, I declare some constants from navigation props. However, I cannot access these constants in the rest of my class. I could just make more variables this.data = data and this.key = key...but that would be rather redundant.

  constructor(props){
    super(props);
    const {navigate} = this.props.navigation;
    const { name, who, what, time, date, where, key } = this.props.navigation.state.params;
    this.date = date;
    this.key = key;
    this.state = {
      changed: false,
      saving: false,
      cancel: false,
      curTitle: name,
      curWho: who,
      curTime: time,
      curDate: date,
      curDescription: what,
      curWhere: where
    }
    console.ignoredYellowBox = ['Setting a timer'];
    console.log(this.date + " " + this.key);
  }   

Upvotes: 2

Views: 5310

Answers (1)

locropulenton
locropulenton

Reputation: 4853

The vars are binded properly.

You probably have to bind the functions you want to access those vars:

....
constructor(props){
  super(props);
  this.something = 'foo';
  this.doSomething = this.doSomething.bind(this);
}
doSomething() {
  console.log(this.something); // will show 'foo'
}
...

Hope this helps.

Upvotes: 2

Related Questions