Skip to content

Commit 0c0f114

Browse files
authored
docs: fix emulator setup example to prevent double-initialization (#734)
* docs: fix emulator setup example to prevent double-initialization Calling connect*Emulator() in a React component body causes 'Firestore has already been started' errors on re-renders. Show two correct patterns: module-level initialization (preferred) and a useRef guard for component-level initialization. Fixes #459 * docs: drop component-level useRef emulator example, recommend module-level init * docs: note that firebaseConfig= prop makes emulator setup difficult, prefer firebaseApp=
1 parent ebc87af commit 0c0f114

1 file changed

Lines changed: 24 additions & 20 deletions

File tree

‎docs/use.md‎

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -97,37 +97,41 @@ SDK not found. useSdk must be called from within a provider
9797

9898
### Connect to the Firebase Local Emulator Suite
9999

100-
Connect a product SDK to the emulator before passing it to a provider. For example, to connect to the Auth and Realtime Database emulators:
100+
Connect emulators to the SDK instances **before** passing them to providers. The safest pattern is to initialize outside of React entirely, which guarantees `connect*Emulator()` only runs once, regardless of re-renders or remounts:
101101

102102
```jsx
103-
import { getAuth, connectAuthEmulator } from 'firebase/auth'; // Firebase v9+
104-
import { getDatabase, connectDatabaseEmulator } from 'firebase/database'; // Firebase v9+
103+
import { initializeApp } from 'firebase/app';
104+
import { getAuth, connectAuthEmulator } from 'firebase/auth';
105+
import { getFirestore, connectFirestoreEmulator } from 'firebase/firestore';
105106

106-
import { FirebaseAppProvider, DatabaseProvider, AuthProvider, useFirebaseApp } from 'reactfire';
107+
import { FirebaseAppProvider, FirestoreProvider, AuthProvider } from 'reactfire';
107108

108-
function FirebaseComponents({ children }) {
109-
const app = useFirebaseApp();
110-
const database = getDatabase(app);
111-
const auth = getAuth(app);
109+
const app = initializeApp(firebaseConfig);
110+
const auth = getAuth(app);
111+
const firestore = getFirestore(app);
112112

113-
// Check for dev/test mode however your app tracks that.
114-
// `process.env.NODE_ENV` is a common React pattern
115-
if (process.env.NODE_ENV !== 'production') {
116-
// Set up emulators
117-
connectDatabaseEmulator(database, 'localhost', 9000);
118-
connectAuthEmulator(auth, 'http://localhost:9099');
119-
}
113+
// Check for dev/test mode however your app tracks that.
114+
// `process.env.NODE_ENV` is a common React pattern
115+
if (process.env.NODE_ENV !== 'production') {
116+
connectAuthEmulator(auth, 'http://localhost:9099');
117+
connectFirestoreEmulator(firestore, 'localhost', 8080);
118+
}
120119

120+
function App() {
121121
return (
122-
<AuthProvider sdk={auth}>
123-
<DatabaseProvider sdk={database}>
124-
<MyCoolAppThatUsesAuthAndRealtimeDatabase />
125-
</DatabaseProvider>
126-
</AuthProvider>
122+
<FirebaseAppProvider firebaseApp={app}>
123+
<AuthProvider sdk={auth}>
124+
<FirestoreProvider sdk={firestore}>
125+
<MyCoolApp />
126+
</FirestoreProvider>
127+
</AuthProvider>
128+
</FirebaseAppProvider>
127129
);
128130
}
129131
```
130132

133+
This pattern requires passing `firebaseApp={app}` to `FirebaseAppProvider` so you own the app instance at module level. If you are currently using `firebaseConfig={...}` on `FirebaseAppProvider`, switch to initializing the app yourself with `initializeApp()` and passing it via `firebaseApp=`. The `firebaseConfig=` prop is a convenience shortcut, but it gives reactfire ownership of the app instance, which makes emulator setup (and anything else that needs the app before React renders) unnecessarily difficult.
134+
131135
Learn more about the Local Emulator Suite in the [Firebase docs](https://firebase.google.com/docs/emulator-suite/connect_and_prototype).
132136

133137
### Set up App Check

0 commit comments

Comments
 (0)