Skip to content

Instantly share code, notes, and snippets.

@dPacc
Created October 8, 2023 15:24
Show Gist options
  • Save dPacc/030b386c0f598693ccc2bc1a9a417201 to your computer and use it in GitHub Desktop.
Save dPacc/030b386c0f598693ccc2bc1a9a417201 to your computer and use it in GitHub Desktop.
React Native Env Vars Setup

The best way to handle environment variables in React Native is to use the react-native-config library.

Here are the steps:

  1. Install the library

npm install react-native-config

Or if you use Yarn:

yarn add react-native-config

  1. Create .env file

Then you can create a .env file on the root of your project and add your environment variables like this:

API_URL=https://myapi.com

If you have different environments (like production, staging, development), you can create different .env files like .env.production, .env.staging etc.

  1. Link the library

For Android, open android/app/build.gradle and add the following at the top

apply from: project(":react-native-config").projectDir.getPath() + "/dotenv.gradle"

For iOS, open the ios project folder in xcode and open Pods>Podfile and add react-native-config dependency in IOS

pod 'react-native-config/Extension', :path => '../node_modules/react-native-config'

Refer: https://medium.com/@sathishkcontact/managing-multiple-environments-in-react-native-android-ios-scripts-for-different-builds-ea4c5bff6782

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment