-
Notifications
You must be signed in to change notification settings - Fork 937
getReactNativePersistence breaks React Native Fast Refresh #6231
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
It may be that this only occurs during debugging but I can't confirm that at the moment now due to a major overhaul in our tooling. |
@slapbox does this still happen for you? |
|
Yes, it does still happen, even with Flipper.
|
The problem is that In this particular case, |
Thanks for your reply @sam-gc. Our file that calls This is the full file that calls When the app is reloaded, import { initializeAuth } from 'firebase/auth';
import { getReactNativePersistence } from 'firebase/auth/react-native';
import AsyncStorage from '@react-native-async-storage/async-storage';
export const afterFirebaseInitialize = firebaseApp => {
initializeAuth(firebaseApp, {
persistence: getReactNativePersistence(AsyncStorage),
});
}; |
When this gets reloaded, |
It shouldn't get reloaded as a new object in the code we have, but it could be - I assume must be if the only possible cause for this issue is It's hard to know for sure though because the issue doesn't occur 100% of the time now with Flipper as our debugging tool (as opposed to react-native-debugger) and the quirks of React Native debugging. Our code looks like this: let firebaseApp;
const makeFirebase = config => {
if (firebaseApp) return firebaseApp;
firebaseApp =
typeof global.window !== 'undefined' ? initializeApp(config) : null;
if (typeof afterFirebaseInitialize === 'function')
afterFirebaseInitialize(firebaseApp);
return firebaseApp;
}; |
That's right -- this error can only be thrown if initializeAuth is called twice, with different objects in the dependencies. Fixing this feature for React Native Fast Refresh is not on our roadmap at the moment, but we can keep this issue open to track the request |
@sam-gc why would the line Something doesn't add up unless there's more to the story here. |
This remains an issue and we're definitely not initializing anything twice. It's painstaking to be without Fast Refresh, and it's apparent we're not the only ones with the issue based on the emojis on @sam-gc's reply. It only began with We call This is it. This is the sole place it's called. I have went over this back when I opened the issue and again now, and there's nothing on our end that's not being done according to the way recommended in the docs. export const afterFirebaseInitialize = firebaseApp => {
initializeAuth(firebaseApp, {
persistence: getReactNativePersistence(AsyncStorage),
});
}; I'd appreciate it so much if this was given some attention. Our codebase is used on Electron and on React Native.
@sam-gc as you can see in oru code example, this is not the case unless again the problem relies with Firebase - or with AsyncStorage, but that's passed to |
I've avoided updating my version of firebase due to this issue. I hope it is resolved soon because it defeats the purpose of using react native. |
Friendly bump @sam-gc. I hope this will not go neglected. It's hard for me to fathom fixing a newly introduced bug not being "on the roadmap." An undocumented regression without a workaround is a bug that needs fixing in my book. |
If it's true that the explanation for this is users double invoking import { initializeApp } from 'firebase/app';
import { initializeAuth } from 'firebase/auth';
import { getReactNativePersistence } from 'firebase/auth/react-native';
import AsyncStorage from '@react-native-async-storage/async-storage';
let initialized = false;
const persistence = {
persistence: getReactNativePersistence(AsyncStorage),
};
export const afterFirebaseInitialize = firebaseApp => {
if (initialized) return;
initialized = true;
initializeAuth(firebaseApp, persistence);
};
let firebaseApp;
const makeFirebase = config => {
if (firebaseApp) return firebaseApp;
firebaseApp = initializeApp(config);
afterFirebaseInitialize(firebaseApp);
}; The explanation that this is a React Fast Refresh problem would hold water if this didn't only affect Firebase on React Native. No other projects on any other platform break like this, and crucially, nor did Firebase used to break like this with version 8 and below. Even more crucially, Firebase 9 used to work as intended for us. I'll have to go back and try to figure out more about why that may be - too late right now. |
Oh! I just notiiced you're importing from both
and get rid of the |
Hey @slapbox. We need more information to resolve this issue but there hasn't been an update in 5 weekdays. I'm marking the issue as stale and if there are no new updates in the next 5 days I will close it automatically. If you have more information that will help us get to the bottom of this, just add a comment! |
Just stumbled upon this thread and I happen to have the same code as @hsubox76 suggested. I imported |
Can you show all of your relevant code, including all Firebase imports and where you call initializeApp and initializeAuth? |
Code import { initializeApp } from 'firebase/app';
import AsyncStorage from '@react-native-async-storage/async-storage';
import {
initializeAuth,
getReactNativePersistence
} from 'firebase/auth/react-native';
// Replace with real values
const firebaseConfig = {
apiKey: "apiKey",
authDomain: "authDomain",
projectId: "projectId",
storageBucket: "storageBucket",
messagingSenderId: "messagingSenderId",
appId: "appId"
};
const firebaseApp = initializeApp(firebaseConfig);
const firebaseAuth = initializeAuth(firebaseApp, {
persistence: getReactNativePersistence(AsyncStorage)
}); |
I'm not totally sure how React Native fast refresh works but it seems like the other user in this thread had to put in some kind of check to make sure that
Again I'm not sure what's needed for React Native fast refresh, or what it keeps or resets when it refreshes. If you wanted to test it you could put a console.log right next to |
I haven't gotten a chance to try @hsubox76's suggestion because I'm pretty sure we can't use it. Our code is cross-platform, so I don't think we can easily implement the suggestion, but maybe via |
@hsubox76 I noticed the checks and I've tried it before commenting but it didn't work. I added the However, this worked for me import { getAuth, initializeAuth, getReactNativePersistence} from "firebase/auth/react-native";
import AsyncStorage from "@react-native-async-storage/async-storage";
if (getApps().length === 0) {
app = initializeApp(firebaseConfig);
auth = initializeAuth(app, {
persistence: getReactNativePersistence(AsyncStorage),
});
} else {
app = getApp();
auth = getAuth(app);
} https://github.com/firebase/firebase-js-sdk/issues/6050#issuecomment-1119131377 |
Sorry to hijack this thread but I'm trying to make sure we correct our documentation to be clear about how to use |
@hsubox76 thanks for your continued attention on this issue.
I first became aware of it from comments on issue #6050, which do link to some mentions in the API reference - specifically here and in comments below. |
this í my sovle. use {initializeAuth} from 'firebase/auth/react-native' replace for {getAuth} from "firebase/auth";
|
This seems to be fixed on version 10.0.0 thank you @hsubox76 Sidenote for anyone following this thread, remove this fix if implemented as it seems to break fast refreshing otherwise
|
Thanks for the update. I'll close this issue for now and add a link to the release notes for 10.0.0 to make it clear that
https://firebase.google.com/support/release-notes/js#version_1000_-_july_6_2023 |
Describe your environment
Describe the problem
[email protected] Auth requires
getReactNativePersistence
on React Native - but including the relevant code forgetReactNativePersistence
causes the auth listener logic in our app to fire every time a change is made, crashing our app (without apparent error message) and preventing us from using Fast Refresh at all.Steps to reproduce:
Unsure of full steps that may be required. For us, including the code below triggers it in logic that otherwise worked in [email protected] and in [email protected] for a while.
Relevant Code:
The text was updated successfully, but these errors were encountered: