There's a requirement to switch between production/development environment while app is running (in development mode). I'm using react-native-dotenv third-party in order to support .env files. I've found a solution that it's possible in nodejs environment like this approach. does anyone know that how is that possible in react native? or is there any other clean alternative?
1 Answer
This feature is already built-in react-native-dotenv. Here is the precise Link for official documentation. In addition, it's completely up to you how you manage multiple .env files. You can use either dotenv-flow or react-native-dotenv.
4 Comments
Aliunco
Thank you so much, but the requirement is to switch between the .env files in run time (while app is running). is there any solution for that?
Asmat ullah
Do you want to change .env while the app is in the production phase like a signed APK or do you want to change it while app is connected to the local server?
Aliunco
No! (thanks for following). let me rephrase it again. while the app is running in the development mode (like running via Expo go), I need to switch between the production and development environments (without restarting the bundler) so the testers can switch between them and test the app.
Asmat ullah
As far as I know, it's kind of impossible (at least for me) because every time we do a change in .env which is in .gitignore we have to tell the metro server manually to publish changes to the App. You could manipulate .env with a component that will change the required .env to
dev or prod at run time via the toggle button in a separate screen(just for testing purposes). i.e toggle===off ? setStates of dev : setStates of prod