React-Native中使用reactnativeconfig配置生产开发测试环境

登录https://js.coach/
搜索这个
安装
yarn add react-native-config
npm install react-native-config
手动link(见网站上的方法)

不需要配置因为会自动link

IOS配置
(cd ios; pod install)
额外的安卓配置

这个文件android/app/build.gradle
最后面加上

apply from: project(':react-native-config').projectDir.getPath() + "/dotenv.gradle"
根目录创建.env文件

API_URL=https://myapi.com写入这一段

App.js中导入并测试
import Config from "react-native-config";
const App = () => {
  return (
    <>
      <StatusBar barStyle="dark-content" />
      <SafeAreaView>
        <ScrollView
          contentInsetAdjustmentBehavior="automatic"
          style={styles.scrollView}>
          <View>
            <Text>{Config.API_URL}</Text>
          </View>
        </ScrollView>
      </SafeAreaView>
    </>
  );
};

显示API_URL=https://myapi.com配置完毕

评论

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

这个站点使用 Akismet 来减少垃圾评论。了解你的评论数据如何被处理