React Native 中 Java 单例的等价物是什么? [英] What is the equivalent of Java singleton in React Native?

查看:44
本文介绍了React Native 中 Java 单例的等价物是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

其实我是一个安卓原生开发者.我是 React-Native 的新手.在 Java 中,我将用户数据和其他数据保存在 Singleton 类中,并从其他活动访问.然后我如何在 React Native 中实现这个过程,以访问来自其他组件的数据或 React Native 中单例的任何替代方案.

Actually I am a Android Native Developer. I am new to React-Native. In Java i keep the users data and others data in Singleton Class and access from other activities. Then how i achieve this process in React native for access data's from other components or any alternative of singleton in React native.

我只是尝试这个,但我收到了类似 getInstance() 未定义的错误.

I just try this but i received the error like getInstance() not defined.

class AppDelegate {
log = "This is AppDelegate";  

  static myInstance = null;
  static getInstance() {
    if (AppDelegate.myInstance == null) {
      AppDelegate.myInstance = new AppDelegate();
    }

    return this.myInstance;
  }

}

谢谢.

推荐答案

导入被缓存,所以如果你导出一个类,这个类会被缓存,如果你导出一个实例,实例将被缓存

Imports are cached, so if you export a class, the class will be cached, if you export an instance, the instance will be cached

这篇关于React Native 中 Java 单例的等价物是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆