config.js
AsyncStorage.getItem("storedValue").then(value=>{
//return data based on the value from asyncStorage storage
if(value==="value1"){
return {
text:"text1",
number:"number1"
}
}
else if(value==="value2"){
return {
text:"text2",
number:"number2"
}
}
})
index.js
import config from "./config"
//I have to use data from config file here
console.log(config.text) //->getting error
//The problem is that, since fetching from AsyncStorage is asynchronous, data from config is not yet available here
//I have tried SyncStorage npm package for react native. It loads all the asyncStored values into memory upon calling
await SyncStorage.init() //-> called on app initialisation
//And can be used later in place of AsyncStorage.getItem(""), which returns the value directly instead of promise
const value=SyncStorage.get("storedValue")
But the problem is, I am accessing the config in index.js(which is the starting file for a react-native project). And I have to initialise SyncStorage here, which is again asynchronous, which means the config file will get loaded before init() happens.
What I am expecting is:
- get the value stored in local storage instantly without async/await in index.js
OR - In the app, I will have a button to restart the app. But, should be able to pass a value during the restart, which can be used in config.js or index.js while the app loads
OR - Or use any other package for deep-linking or something like that, so that on app restarting app using deep-link, the value passed should be accessible. It has to work for both android and iOS
Changes that are not possible:
- Cannot make any other file other than index.js as starting file because, many other project packages are being initialized here, and in fact, the stored value we are trying to access needs to be passed to these packages while initializing.
Thank you.