singleton object in react native

前端 未结 5 1888
慢半拍i
慢半拍i 2020-12-13 08:56

I\'m new in react native.I want store multiple small small strings to common singleton object class and want to access it from singleton object for all component. Can anyone

5条回答
  •  执念已碎
    2020-12-13 10:02

    There is a workaround for this, react native packager require all the modules in the compilation phase for a generating a bundle , and after first require it generates an internal id for the module, which is from then on referenced in the whole run-time memory , so if we export an instance of a class from the file, that object will be referenced every-time whenever that file is imported .

    TLDR;

    Solution I :

    class abc {
    
    }
    
    
    module.exports = new abc()
    

    Solution II : I assume you want to get your strings which are static and wont change , so you can declare them as static and access them directly with class name

    FYI :this works with webpack also.

提交回复
热议问题