I followed the instructions from this guide, which seemed straightforward, but I keep encountering the error:
error while setting analytics user id: [Error: No Firebase App '[DEFAULT]' has been created - call firebase.initializeApp()]
This happens when trying to set the user ID in my App.js. Here is the relevant snippet from my App.js file:
import { auth } from './firebase.js'
import analytics from '@react-native-firebase/analytics';
const App = () => {
const [currentUser, setCurrentUser] = useState(null);
useEffect(() => {
(async () => {
if(currentUser){
console.log("I have current user: ", analytics())
try{
await analytics().setUserId(currentUser.uid)
console.log("all done")
}catch(error){
console.log("error while setting analytics user id: ", error);
}
}
})();
}, [currentUser]);
useEffect(() => {
const unsubscribe = auth.onAuthStateChanged((user) => {
if (user) {
setCurrentUser(user);
}
});
return () => unsubscribe();
}, []);
if(!currentUser){
return (
<NavigationContainer>
<AuthNavigator />
</NavigationContainer>
);
}
else{
return (
<NavigationContainer>
<MainNavigator />
</NavigationContainer>
);
}
};
export default App;
Below is my firebase.js file where I initialize Firebase using the standard firebase/compat package:
import firebase from "firebase/compat/app";
import 'firebase/compat/firestore'
import 'firebase/compat/auth'
const firebaseConfig = {
apiKey: "some_key",
authDomain: "some_domain",
databaseURL: "some_url",
projectId: "some_project_id",
storageBucket: "some_id",
messagingSenderId: "id",
appId: "id",
measurementId: "id"
};
if (firebase.apps.length === 0) {
firebase.initializeApp(firebaseConfig);
}
const authState = firebase.auth();
export const auth = authState;
export const firestore = firebase.firestore();
I spent a lot of time troubleshooting and followed the docs correctly, so I don't understand why the analytics module complains about the missing default app. I suspect the issue might be due to mixing the firebase/compat initialization in firebase.js with the @react-native-firebase Analytics usage in App.js.
I'm looking for a practical solution to ensure that the Firebase app is correctly initialized so that analytics can detect the default app. Please note, I'm not looking for generic documentation links—I need a practical fix to resolve this error.