Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React Native DEV and PROD variables

Tags:

react-native

How do I know if my React Native app is running in production or development? Is there some sort of way within JavaScript to tell? Is there a global that is passed in?

like image 835
Ryan McDermott Avatar asked Oct 21 '15 16:10

Ryan McDermott


People also ask

How does React Native handle environment variables?

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.

What is __ Dev __ React Native?

You can use the __DEV__ global variable in JavaScript to determine if you're using React Native packager or not. If you are running your app in the iOS Simulator or Android emulator __DEV__ will be set to true . https://reactnative.dev/docs/javascript-environment. Follow this answer to receive notifications.

How do I view environment variables in React Native?

An environment variable is a variable who's value is set from outside the program. This variable is made up of a key/value pair that can be accessed simply by importing the variable from source file of your React Native App. In some cases, such as in React, the variable doesn't need to be imported at all.


3 Answers

You can use the __DEV__ global variable in JavaScript to determine if you're using React Native packager or not. If you are running your app in the iOS Simulator or Android emulator __DEV__ will be set to true.

https://reactnative.dev/docs/javascript-environment

like image 160
Austin Avatar answered Oct 03 '22 18:10

Austin


When the __DEV__ variable is set to true, it turns on a bunch of useful development warnings. For production, it is recommended to set __DEV__=false.

like image 40
Abhishek Kumar Avatar answered Oct 03 '22 17:10

Abhishek Kumar


You can use the __DEV__ variable. By default if you run your app with npx react-native run-ios or npx react-native run-android, it will run in Debug mode and __DEV__ will be true. In release mode, __DEV__ will be false. You can use it this way:

const CLOUD_API_BASE_URL = __DEV__ ? 'https://api-dev.yourdomain.com' : 'https://api-prod.yourdomain.com';

You can run the app in Release mode with the terminal: react-native run-android --variant release #android react-native run-ios --configuration Release #ios

Or open the ios folder in XCode, choose Product > Scheme > Edit Schemes xcode edit scheme Select Run in the left menu. For Build Configuration, choose 'Release' and uncheck 'Debug executable'

enter image description here

In Android Studio, similarly, you can set the build variant to release enter image description here

like image 20
Raphael Pinel Avatar answered Oct 03 '22 18:10

Raphael Pinel