2016-02-22 16:19:01 -08:00
|
|
|
# Config variables for React Native apps
|
|
|
|
|
|
|
|
Module to expose config variables to your javascript code in React Native, supporting both iOS and Android.
|
|
|
|
|
|
|
|
Bring some [12 factor](http://12factor.net/config) love to your mobile apps!
|
|
|
|
|
|
|
|
|
|
|
|
## Usage
|
|
|
|
|
|
|
|
Declare config variables in `.env`:
|
|
|
|
|
|
|
|
```
|
|
|
|
API_URL=https://myapi.com
|
|
|
|
LOG_ERRORS=true
|
|
|
|
```
|
|
|
|
|
2016-02-22 16:29:33 -08:00
|
|
|
Then access from your app:
|
2016-02-22 16:19:01 -08:00
|
|
|
|
|
|
|
```js
|
2016-02-22 16:29:33 -08:00
|
|
|
import Config from 'react-native-config'
|
2016-02-22 16:19:01 -08:00
|
|
|
|
|
|
|
Config.API_URL // 'https://myapi.com'
|
|
|
|
Config.SHOW_ERRORS // 'true'
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
## Setup
|
|
|
|
|
2016-02-22 17:27:59 -08:00
|
|
|
Install the package:
|
|
|
|
|
|
|
|
```
|
|
|
|
$ npm install react-native-config --save
|
|
|
|
```
|
|
|
|
|
|
|
|
Then follow the platform-specific instructions below:
|
|
|
|
|
|
|
|
|
|
|
|
### iOS
|
|
|
|
|
2016-02-23 16:42:53 -08:00
|
|
|
Link the library with [rnpm](https://github.com/rnpm/rnpm):
|
2016-02-23 16:07:33 -08:00
|
|
|
|
2016-02-23 16:42:53 -08:00
|
|
|
```
|
|
|
|
$ rnpm link react-native-config
|
|
|
|
```
|
2016-02-23 16:07:33 -08:00
|
|
|
|
2016-02-22 17:27:59 -08:00
|
|
|
|
2016-02-22 20:05:00 -08:00
|
|
|
### Android
|
|
|
|
|
|
|
|
Include this module in `android/settings.gradle`:
|
|
|
|
|
2016-02-22 17:27:59 -08:00
|
|
|
```
|
2016-02-23 16:07:33 -08:00
|
|
|
include ':react-native-config'
|
2016-02-22 20:05:00 -08:00
|
|
|
include ':app'
|
|
|
|
|
2016-02-23 16:07:33 -08:00
|
|
|
project(':react-native-config').projectDir = new File(rootProject.projectDir,
|
|
|
|
'../node_modules/react-native-config/android')
|
2016-02-22 17:27:59 -08:00
|
|
|
```
|
|
|
|
|
2016-02-22 20:05:00 -08:00
|
|
|
Apply a plugin and add dependency to your app build, in `android/app/build.gradle`:
|
2016-02-22 17:27:59 -08:00
|
|
|
|
|
|
|
```
|
2016-02-23 16:07:33 -08:00
|
|
|
// 2nd line, add a new apply:
|
2016-02-22 20:05:00 -08:00
|
|
|
apply from: project(':react-native-config').projectDir.getPath() + "/dotenv.gradle"
|
|
|
|
|
2016-02-23 16:07:33 -08:00
|
|
|
// down below, add new compile:
|
2016-02-22 20:05:00 -08:00
|
|
|
dependencies {
|
|
|
|
...
|
|
|
|
compile project(':react-native-android-config')
|
|
|
|
}
|
2016-02-22 17:27:59 -08:00
|
|
|
```
|
|
|
|
|
2016-02-22 20:05:00 -08:00
|
|
|
Change your main activity to add a new package, in `android/app/src/main/.../MainActivity.java`:
|
2016-02-22 16:29:33 -08:00
|
|
|
|
2016-02-22 20:05:00 -08:00
|
|
|
```java
|
2016-02-23 16:07:33 -08:00
|
|
|
import com.lugg.ReactNativeConfig.ReactNativeConfigPackage; // add import
|
2016-02-22 20:05:00 -08:00
|
|
|
|
|
|
|
public class MainActivity extends ReactActivity {
|
|
|
|
// ...
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected List<ReactPackage> getPackages() {
|
|
|
|
return Arrays.<ReactPackage>asList(
|
|
|
|
new MainReactPackage(),
|
|
|
|
new ReactNativeConfigPackage() // add package
|
|
|
|
);
|
|
|
|
}
|
|
|
|
```
|