基本上,React Native只能在开发和生产之间切换。
__DEV__
React Native将一个名为
https://github.com/facebook/react-native/tree/master/packager#pathtomodulenamemapbundle-query-params
查看并切换设置。
顺便说一句,如果您在Android上执行
react-native-config
到目前为止,
,但是如果您想要将阶段和生产分开,则仅靠主体机制是不可能的。
https://github.com/facebook/react-native/issues/4017
打包程序中没有暂存或生产选项,您必须在运行时
react-native-config是一个软件包,用于将其与此处分开。
https://github.com/luggit/react-native-config
机制是在构建时指定环境,将设置值嵌入本机应用程序中,并在运行时桥接它,以便可以读取它。
自述文件的编写非常仔细,因此您可以阅读它,但是我将代替备忘录来写下设置方法。
https://github.com/luggit/react-native-config#different-environments
react-native-config读取名为
首先,如下创建环境和配置文件。
<表格>
tr>
header>
<身体>
tr>
tr>
tr>
tbody>
table>
.env
1 | API_ENDPOINT=http://dev.example.com/api/ |
.env.staging
1 | API_ENDPOINT=http://stg.example.com/api/ |
.env。生产
1 | API_ENDPOINT=http://prd.example.com/api/ |
安卓
对于Android,只需在指定环境变量
1 | ENVFILE=.env.staging react-native run-android |
iOS
对于iOS,有必要为每个环境创建一个方案。
-
名称易于理解,例如
appname-staging