简体   繁体   中英

How to make sure there is not multiple instances of imported modules

Currently making a RN app and I will integrate this with Google Analytics and Firebase. I will use this module: https://github.com/idehub/react-native-google-analytics-bridge

I am building a wrapper class/module that will be contacting every provider so I can extend my own in the future easily.

My structure is as follows (each class extends the other): React Component -> BaseComponent -> (Specific Implementation) -> ScreenComponent

Since a screen can have multiple components and I will put the analytics wrapper in my BaseComponent there will be multiple calls to the analytics module I am building.

I am still a little new into this require/exports coming from Java development so to me there will be multiple instances of this module creating multiple sessions.

I was thinking on making the analytics wrapper a global object, to make sure the setup code is only run once, and the session is being kept.

Any input appreciated.

Both nodejs native require and requirejs automatically handles this.

Beware, this is two edged sword - changing properties of imported module also changes them in all other scripts that import them, similarly to changing Java static property.

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