简体   繁体   中英

React Native - Accessing static variable on same class

I just moved from android to React Native recently. So need some help. Why I not able to access the variable on same class, for example when I call URL_API_SERVER from another class, it give me 'Undefined/api/v2'.

class Constant {
    static BASE_URL = 'https://xxxxx';
    static URL_API_SERVER = this.BASE_URL + '/api/v2';
    static STATIC_BASEURL = this.BASE_URL + '/static';
    static URLSTRING_FAQ = this.STATIC_BASEURL + '/FAQ.html';
    static URLSTRING_TOU = this.STATIC_BASEURL + '/TOU.html';
}

export default Constant;

Since you are using static variable, you cannot use this . You can access the static variable like below.

class Constant {
    static BASE_URL = 'https://xxxxx';
    static URL_API_SERVER = Constant.BASE_URL + '/api/v2';
    static STATIC_BASEURL = Constant.BASE_URL + '/static';
    static URLSTRING_FAQ = Constant.STATIC_BASEURL + '/FAQ.html';
    static URLSTRING_TOU = Constant.STATIC_BASEURL + '/TOU.html';
}

export default Constant;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM