React Native – How to use .env file?

Use react-native-dot-env

  • Install package
yarn add react-native-dotenv
  • Edit .babelrc
{
  "plugins": [
    ["module:react-native-dotenv"]
  ]
}
  • Create .env file
API_BASE_URL = 
API_KEY = 
  • Usage
import { API_BASE_URL, API_KEY } from '@env';

Use react-native-config

Android

  • How to use env file for strings.xml?
CODEPUSH_DEPLOYMENT_ANDROID=xxxxxxxxxxxxxxxx
CODEPUSH_DEPLOYMENT_IOS=yyyyyyyyyyyyyyyyyyy
<?xml version="1.0" encoding="UTF-8"?>
<resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
...
    <string moduleConfig="true" name="CodePushDeploymentKey">@string/CODEPUSH_DEPLOYMENT_ANDROID</string>
...
</resources>

iOS

  • How to use env file for info.plist?

https://github.com/luggit/react-native-config#availability-in-build-settings-and-infoplist

Be the first to comment

Leave a Reply

Your email address will not be published.


*