在我的react native应用程序中,我目前有一个用户类,我在其中定义了一个当前用户,如下所示:

class User {
    static currentUser = null;

    //other relevant code here

    static getCurrentUser() {
        return currentUser;
    }
}

export default User;

在另一个类中,我试图访问此currentUser的设置值.我不知道如何正确调用这个函数;我得到了错误User.getCurrentUser is not a function.我应该用另一种方式调用这个函数吗?

var User = require('./User');

getInitialState: function() {

    var user = User.getCurrentUser();

    return {
        user: user
    };


},

推荐答案

你正在混合imp或t/exp或t种风格.您应该将导入更改为

var User = require('./User').default

imp或t User from './User'

Or change your exp或t:

module.exp或ts = User

React-native相关问答推荐

如何重复使用expo 音频对象S录音?

无法解析 com.google.gms:google-services:4.3.15

何时在 React Native 中使用辅助功能 Role='link'?

按下按钮有一个borderRadius

更改所选标签栏元素的底部边框 colored颜色

React-Native 是单线程执行还是多线程执行?

TouchableOpacity 作为 ListView 中的 Item 仅在 TextInput 失go 焦点后做出响应

在 React Native 中计算两个 lat+lng 坐标之间的距离?

如何将抖动动画添加到视图(react-native)?

迷失在 Ionic、Cordova 等混合应用程序框架中

react-native - 如何在过go 两个文件夹中导入组件?

React Native:无法点击调试菜单

如何删除不需要的expo 模块

使用离线包在 iOS 设备上运行 react-native 应用程序

如何在 React Native 中获取电话号码?

查找文本输入框和按钮字段的资源名称以响应本机 android 应用程序

React Native - 如何从推送通知中打开路由

如何在react-native中关闭模态

com.facebook.react.bridge.readablenativemap 不能转换为 java.lang.string

发布未使用 JavaScript 代码更新的 APK