Environment variables in React Native allow you to store configuration values such as API URLs, keys, and feature flags outside your source code. This helps keep sensitive data secure and makes your app easier to configure for different environments like development, staging, and production.
.env files.React Native does not support environment variables natively like Node.js. External libraries such as react-native-config or Babel plugins are used to inject variables during the build process.
// .env file storing environment-specific values
API_URL=https://api.example.com
APP_ENV=development
// Importing environment variables using react-native-config
import Config from "react-native-config";
console.log(Config.API_URL);
console.log(Config.APP_ENV);
The console will print the API URL and environment name defined in the .env file. These values change automatically when you switch environment files.
This tool simulates how react-native-config reads your .env file and makes variables available in JavaScript. Edit the file on the left and click "Simulate Build".
Think of environment variables as hidden switches. When you build the app, React Native reads these switches and locks the values into the app bundle. Changing the switch later requires rebuilding the app.
.env files for each environment..env file.APP_NAME.