第一步安装react-native-config
yarn add react-native-config
npm i react-native-config
第二步在根目录创建.env
API_URL=http://dev.example.com
在应用里面使用
import { StyleSheet, Text, View } from 'react-native'
import React from 'react'
import Config from 'react-native-config'
const App = () => {
return (
<View>
<Text>App---{Config.API_URL}</Text>
</View>
)
}
const styles = StyleSheet.create({})
export default App
Android配置需要在android目录下的app文件里的build.gradle添加
apply from: project(':react-native-config').projectDir.getPath() + '/dotenv.gradle'
ios配置需要
cd iOS
pod install