从React-Native应用程序中的另一个类访问静态变量? [英] Access static variable from another class in React-Native app?
本文介绍了从React-Native应用程序中的另一个类访问静态变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在我的react-native应用程序中,我目前有一个User类,我在其中定义当前用户如下:
in my react-native app I currently have a User class in which I define a current user as below:
class User {
static currentUser = null;
//other relevant code here
static getCurrentUser() {
return currentUser;
}
}
export default User;
在另一个类中,我试图访问此currentUser的设定值。我无法弄清楚如何正确调用此函数;我收到错误 User.getCurrentUser不是函数
。我应该以不同的方式调用此函数吗?
In a different class, I am trying to access the set value of this currentUser. I cannot figure out how to correctly call this function; I am getting the error User.getCurrentUser is not a function
. Should I be calling this function in a different way?
var User = require('./User');
getInitialState: function() {
var user = User.getCurrentUser();
return {
user: user
};
},
推荐答案
您正在混合 import
/ export
样式。您应该将导入更改为
You are mixing import
/ export
styles. You should either change your import to
var User = require('./User').default
或
import User from './User'
或更改您的出口:
module.exports = User
这篇关于从React-Native应用程序中的另一个类访问静态变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文