> For the complete documentation index, see [llms.txt](https://twins-bamboo.gitbook.io/react-native-note/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://twins-bamboo.gitbook.io/react-native-note/advanced/handling-different-environments.md).

# Handling different environments

根據環境(pro、stage、dev...)而配置環境參數

### &#x20;使用 [React-Native-Config](https://github.com/luggit/react-native-config) 此Library

### **設定步驟參考** [**Setup部分**](https://github.com/luggit/react-native-config#setup)

![](https://bambooooo.gitbooks.io/react-native/content/React%20Native/images/more/rn-config.png)

### **使用的方式 參考** [**Usage部分**](https://github.com/luggit/react-native-config#usage)**（下方只貼上RN內的使用方式）**

**RN**

在RN專案下創建新檔案 .env

```
API_URL=https://myapi.com
GOOGLE_MAPS_API_KEY=abcdefgh
```

獲取定義的參數

```
import Config from 'react-native-config'

Config.API_URL  // 'https://myapi.com'
Config.GOOGLE_MAPS_API_KEY  // 'abcdefgh'
```

Keep in mind this module doesn't obfuscate or encrypt secrets for packaging, so do not store sensitive keys in .env. It's [basically impossible to prevent users from reverse engineering mobile app secrets](https://rammic.github.io/2015/07/28/hiding-secrets-in-android-apps/), so design your app (and APIs) with that in mind.

### **執行（下方只貼上RN內的使用方式）**

將不同的環境變數設定寫在各個檔案內: **.env.stage 、** **.env.dev**&#x20;

react-native-config 預設是讀取 .env ，但當在build或者release app時可以更改

```
$ ENVFILE=.env.staging react-native run-ios           # bash
$ SET ENVFILE='.env.staging' && react-native run-ios  # windows
$ env:ENVFILE=".env.staging"; react-native run-ios    # powershell
```

### &#x20; 參考

* [Managing Configuration in React Native](https://medium.com/differential/managing-configuration-in-react-native-cd2dfb5e6f7b)
