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

0次阅读

共计 601 个字符,预计需要花费 2 分钟才能阅读完成。

登录 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 配置完毕

正文完
 0