Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React Native Expo Environment Variables

So I'm happy with the concept of environment variables as explained in this article and others https://www.freecodecamp.org/news/how-to-gracefully-use-environment-variables-in-a-react-native-app/

Great, I've got my SOMETHING="something" stored so I can just use env.SOMETHING or whatever

The part I'm a little lost on is where you keep the live variables?

I would rather not do a solution like this as it seems you are still keeping your keys quite public and that you are just choosing based on the environment with if statements

Manage environment with expo react native

For example with an Express App deployment we have, we specify

let endPointURL = env.endPointURL

and then we keep a versoin of that variable locally and when it sits with AWS it is overridden by AWS servers as explained here

I was just wondering does something like that exist for Android and iOS builds (on the respective stores) or through Expo?

Thanks all

like image 304
Ash Hogarth Avatar asked Oct 07 '19 12:10

Ash Hogarth


People also ask

How do you use environment variables in React Native?

All you have to do is write or define your variable in your root folder. Then, you can import the variable in your component before calling it. While env variables are very useful in React Native, it is not advisable to use them to store sensitive information like API keys, authentication keys, or passwords.

How do you use Expo in React Native?

Running your React Native applicationInstall the Expo Go app on your iOS or Android phone and connect to the same wireless network as your computer. On Android, use the Expo Go app to scan the QR code from your terminal to open your project. On iOS, use the built-in QR code scanner of the default iOS Camera app.

What is Expo EAS?

Expo Application Services (EAS) are deeply integrated cloud services for Expo and React Native apps, from the team behind Expo. Read the full pitch at expo. dev/eas, or follow the links below to learn how to get started. EAS Build. Compile and sign Android/iOS apps with custom native code in the cloud.

What is env file in React Native?

Using environment file (. env) to configure most critical variables like database credentials, api keys, is the most common practice in software development.


1 Answers

Honestly I think the way they go about it is a little silly. There may be a better way to go about than this, but I think I followed their documentation suggestions.

https://docs.expo.io/versions/latest/distribution/release-channels/#using-release-channels-for-environment-variable-configuration

They have a code snippet suggesting you create a function to look at the release configuration itself.

I interpreted it that you might do something like the code below and store your environment variables in a variables.js file and pull in your environment variables as such.

import Constants from 'expo-constants';

export const prodUrl = "https://someapp.herokuapp.com";

const ENV = {
  dev: {
    apiUrl: "http://localhost:3000"
  },
  staging: {
    apiUrl: prodUrl
  },
  prod: {
    apiUrl: prodUrl
  }
};

function getEnvVars(env = "") {
  if (env === null || env === undefined || env === "") return ENV.dev;
  if (env.indexOf("dev") !== -1) return ENV.dev;
  if (env.indexOf("staging") !== -1) return ENV.staging;
  if (env.indexOf("prod") !== -1) return ENV.prod;
}

export default getEnvVars(Constants.manifest.releaseChannel);

Edit:

Now that Expo supports config file as app.config.js or app.config.ts, we can use the dotenv. Check this: https://docs.expo.io/guides/environment-variables/#using-a-dotenv-file

like image 176
Caleb Davenport Avatar answered Oct 18 '22 23:10

Caleb Davenport