React native: Always running component
You are explaining a design pattern of something called: Singleton.
Here's how to do it:
let instance = null;
class Singleton{
constructor() {
if(!instance){
instance = this;
}
// to test whether we have singleton or not
this.time = new Date()
return instance;
}
}
Testing singleton On above class we have defined a time property to make sure we have singleton working properly.
let singleton = new Singleton()
console.log(singleton.time);
setTimeout(function(){
let singleton = new Singleton();
console.log(singleton.time);
},4000);
If it prints the same time for both the instances, it means we have a working singleton.
Now, you can load your server data asynchronously inside the singleton and do whatever you'd like to do with it. Anytime you try to access the singleton class, it will be the same instance and it will not be instantiated more than once. In other words, it will not be destroyed.
Source: http://amanvirk.me/singleton-classes-in-es6/