score:0

Accepted answer

i have found an github issue in which its suggested to use preloadfirestore from reactfire.

they although provide an example in their sample app:

preloadfirestore(firebaseapp, firestore => { 
  return firestore().enablepersistence(); 
}), 

if you want to use it like they do, you have a basic setup like this:

// create a preload function to combine multiple preloads
const preloadsdks = firebaseapp => {
  return promise.all([
    preloadfirestore(firebaseapp, firestore => {
      return firestore().settings({host: 'localhost:8080', ssl: false});
    }),
    // preloaddatabase(), 
    // preloadstorage(), 
    // etc.
  ]);
};

function app() {
  const firebaseapp = usefirebaseapp();

  // kick off fetches for sdks and data that
  // we know our components will eventually need.
  //
  // this is optional but encouraged as part of the render-as-you-fetch pattern
  // https://reactjs.org/docs/concurrent-mode-suspense.html#approach-3-render-as-you-fetch-using-suspense
  preloadsdks(firebaseapp).then(() => promise.resolve());

  return (
    <yourcomponents />
  )
}

afterwards you can just use usefirestore() anywhere in the app to lazy load it with the settings above.

hints:

  • you have to use the <firebaseappprovider /> before preloading the sdks
  • your component preloading the sdks has to be wrapped inside a <suspense /> or <suspensewithperf /> component
  • check the reactfire demo app to see how you can not only preload sdks but data as well

score:1

thanks for your help guys! reactfire api changed once again, this is currently working solution:


import react from 'react';
import logo from './logo.svg';
import './app.css';
import answers from './components/answers'
import homescreen from './screens/homescreen'
import { preloadfirestore, usefirebaseapp } from 'reactfire'
import firebase from 'firebase'

const preloadsdks = (firebaseapp: firebase.app.app) => {
  return promise.all([
    preloadfirestore({
      firebaseapp,
      setup: firestore => {
      return firestore().settings({host: 'localhost:8080', ssl: false});
    }
    }),
    // preloaddatabase(),
    // preloadstorage(),
    // etc.
  ]);
};

function app() {
  const firebaseapp = usefirebaseapp();

  preloadsdks(firebaseapp).then(() => promise.resolve());

  return (
    <div classname="app">
      <header classname="app-header">
        <img src={logo} classname="app-logo" alt="logo" />
        <homescreen />
      </header>
    </div>
  );
}

export default app;

score:1

the reactfire docs on emulation now differ from what was mentioned in the earlier answers, they were updated august 2021.

i've modified their example to show you how to use the firestore and auth emulators with firebase v9+:

import { getauth, connectauthemulator } from 'firebase/auth'; // firebase v9+
import { getfirestore, connectfirestoreemulator } from 'firebase/firestore'; // firebase v9+

import { firebaseappprovider, databaseprovider, authprovider, usefirebaseapp } from 'reactfire';

function firebasecomponents({ children }) {
  const app = usefirebaseapp();
  const firestore = getfirestore(app);
  const auth = getauth(app);

  // check for dev/test mode however your app tracks that.
  // `process.env.node_env` is a common react pattern
  if (process.env.node_env !== 'production') {
    // set up emulators
    connectfirestoreemulator(firestore, 'localhost', 8080);
    connectauthemulator(auth, 'http://localhost:9099');
  }

  return (
    <authprovider sdk={auth}>
      <firestoreprovider sdk={firestore}>
        <mycoolappthatusesauthandfirestore />
      </firestoreprovider>
    </authprovider>
  );
}

similar connection patterns also exist for other emulators (connectstorageemulator, connectdatabaseemulator etc...)


Related Query

More Query from same tag