title: Correct way to use initialize firestore in react-native expo app
body: <p>Have seen two different ways to initialize firestore in a react-native app and would like to know what the differences between the two are. The method shown <em>in the firestore docs</em> (<a href="https://firebase.google.com/docs/firestore/quickstart#initialize" rel="nofollow noreferrer">https://firebase.google.com/docs/firestore/quickstart#initialize</a>) looks like</p>
  
  <pre class="lang-js prettyprint-override"><code>const admin = require('firebase-admin');
  const functions = require('firebase-functions');
  
  admin.initializeApp(functions.config().firebase);
  
  export fs = admin.firestore();
  </code></pre>
  
  <p>while the "firebase" way (as seen <em>in this expo post</em>: <a href="https://forums.expo.io/t/open-when-an-expo-firebase-firestore-platform/4126/29" rel="nofollow noreferrer">https://forums.expo.io/t/open-when-an-expo-firebase-firestore-platform/4126/29</a>), which is the way I currently use and appears to work, looks like</p>
  
  <pre class="lang-js prettyprint-override"><code>import * as firebase from 'firebase';
  import 'firebase/firestore';//for using firestore functions, see https://stackoverflow.com/a/50684682/8236733
  import { firebaseConfig } from './firebase-credentials';//WARN: gitignored, exports object containing firebase (web)app credentials
  
  
  // Initialize Firebase
  // why in separate file? see https://github.com/zeit/next.js/issues/1999 and https://ilikekillnerds.com/2018/02/solving-issue-firebase-app-named-default-already-exists/
  
  // firebase.initializeApp(firebaseConfig);
  
  try {
      firebase.initializeApp(firebaseConfig)
  
      /*WARN:
      @firebase/firestore:, Firestore (5.0.4): 
      The behavior for Date objects stored in Firestore is going to change
      AND YOUR APP MAY BREAK.
      To hide this warning and ensure your app does not break, you need to add the
      following code to your app before calling any other Cloud Firestore methods:
  
      const firestore = firebase.firestore();
      const settings = {timestampsInSnapshots: true};
      firestore.settings(settings);
  
      With this change, timestamps stored in Cloud Firestore will be read back as
      Firebase Timestamp objects instead of as system Date objects. So you will also
      need to update code expecting a Date to instead expect a Timestamp. For example:
  
          // Old:
          const date = snapshot.get('created_at');
          // New:
          const timestamp = snapshot.get('created_at');
          const date = timestamp.toDate();
  
      Please audit all existing usages of Date when you enable the new behavior. In a
      future release, the behavior will change to the new behavior, so if you do not
      follow these steps, YOUR APP MAY BREAK.
      */
      const fsSettings = {/* your settings... */ timestampsInSnapshots: true};
      firebase.firestore().settings(fsSettings)
  } catch (err) {
      // we skip the "already exists" message which is
      // not an actual error when we're hot-reloading
      if (!/already exists/.test(err.message)) {
          console.error('Firebase initialization error', err.stack)
      }
  }
  
  export const fs = firebase.firestore()
  </code></pre>
  
  <p>The post linked to is the only instance where I could find someone else doing this, but again it does work for me (can read and write to firestore).</p>
  
  <p>Very new to using firebase/firestore and would like to use the more 'correct' method. Is there any difference between initializing firestore in the app in these separate ways?</p>