javascript React-native:检测开发环境或生产环境

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34315274/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 17:32:20  来源:igfitidea点击:

React-native : detect dev or production env

javascriptandroidreact-native

提问by Jules Ivanic

I develop a react-native app where some API calls should be made.

我开发了一个 react-native 应用程序,应该在其中进行一些 API 调用。

If I'm in production mode my app should call, for example, this host :

例如,如果我处于生产模式,我的应用程序应该调用此主机:

https://example.com/

but if I'm in dev mode, it should call my local host machine on a different port, for example :

但是如果我处于开发模式,它应该在不同的端口上调用我的本地主机,例如:

http://192.168.0.10:8080/

How the react-native code can be aware of its environment ?

react-native 代码如何知道它的环境?

.dotenvseems to not work with react-native.

.dotenv似乎不适用于 react-native。

In the Chrome debug, we can see that the application is launched with some params :

在 Chrome 调试中,我们可以看到应用程序启动时带有一些参数:

Running application "AppName" with appParams: {"initialProps":{},"rootTag":1}. __DEV__ === true, development-level warning are ON, performance optimizations are OFF

Can we get this __DEV__value in the application ?

我们可以__DEV__在应用程序中获得这个值吗?

Thanks

谢谢

回答by purii

Set automatically

自动设置

The constant __DEV__is set automatically.

常数__DEV__是自动设置的。

Production

生产

Building the bundle via --dev=falseshould unset __DEV__.

构建 bundle via--dev=false应该 unset __DEV__

Usage

用法

if (__DEV__) {
    console.log('Development');
} else {
    console.log('Production');
}