React-native:检测开发或生产环境 [英] React-native : detect dev or production env

查看:412
本文介绍了React-native:检测开发或生产环境的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开发了一个本机响应应用,其中应该进行一些API调用。

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

如果我处于生产模式,我的应用应该调用,例如,这个主机:

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/

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

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

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

.dotenv seems to not work with react-native.

在Chrome调试中,我们可以看到应用程序是使用一些参数启动的:

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

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

我们可以得到这个吗应用程序中的 __ DEV __ 值?

Can we get this __DEV__ value in the application ?

谢谢

推荐答案

自动设置



自动设置常量 __ DEV __

通过构建捆绑包 - dev = false 应取消设置 __ DEV __

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

这篇关于React-native:检测开发或生产环境的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆