PUROGU LADESU

ポエムがメインのブログです。

【ReactNative】.envを使ってAPIKeyを読み込む

.envを使う

.envファイルからfirebaseのapikeyを取り出したい。

yarn add react-native-dotenv

GitHub - zetachang/react-native-dotenv: A Babel preset let you import application configs from .env file (zero runtime dependency)

// babel.config.js
presets: ['babel-preset-expo', 'module:react-native-dotenv'],
// .env
apiKey=xxxxxxxxxxxxxxxxxxxx
authDomain=xxxxxxxxxxxxxxxxxxxx
....
import {
  apiKey,
  authDomain,
  databaseURL,
  projectId,
  storageBucket,
  messagingSenderId,
  appId,
  measurementId,
} from 'react-native-dotenv';

const options = {
  apiKey,
  authDomain,
  databaseURL,
  projectId,
  storageBucket,
  messagingSenderId,
  appId,
  measurementId,
};

const fire = new Firebase(options);

もしくは

シンボリックリンクを使って切り替えても良いみたい。

  ln -fs app.prod.json app.json
  ln -fs app.dev.json app.json