首次安装时,应用程序运行没有任何问题。
- 在启动画面检查登录状态。
- 用户未登录,导航到登录屏幕。
但 unsubscribe() 不是函数
当应用程序最小化然后重新打开时,错误显示并停留在启动画面。这只发生在最小化 > 从全新安装和首次运行恢复时。
这意味着如果我从后台终止应用程序并打开然后最小化然后恢复,它工作得很好。
这是我在 firebase 文档中使用的 checkLoginState
函数。我不确定这与 react-native
或 firebase
或 react-native-firebase
或 react-navigation
有关.知道如何调试和解决这个问题吗?
Handle the sign-in flow manually
function checkLoginState(event) {
if (event.authResponse) {
// User is signed-in Facebook.
var unsubscribe = firebase.auth().onAuthStateChanged(function(firebaseUser) {
unsubscribe();
// Check if we are already signed-in Firebase with the correct user.
if (!isUserEqual(event.authResponse, firebaseUser)) {
// Sign in user.
} else {
// User is already signed-in Firebase with the correct user.
}
});
} else {
// User is signed-out of Facebook.
firebase.auth().signOut();
}
}
更新
在全新安装 > 首次运行 > 最小化 > 恢复` 中,应用程序看起来像堆栈或什么。如果我使用 react-native reload 重新加载应用程序,我会在日志中得到这个。 componentDidmount 和 unmount 运行多次。每个最小化 > 恢复堆栈 1。如果我终止应用程序并重新打开,这个问题就会消失。请注意,这在物理和虚拟设备中都会发生。
更新 1
这主要与 React Native 有关。 https://github.com/facebook/react-native/issues/12562
最佳答案
我也遇到了这个问题并且对此感到很沮丧...根据调用监听器作为函数的文档必须取消订阅监听器,但事实并非如此。所以这就是我所做的,希望对你也有帮助..
class Home extends Component {
constructor(props) {
super(props);
this.Homelistener = null;
this.state = {
// ...
};
this.Homelistener = firebase.auth().onAuthStateChanged(user => {
if (user) {
//do whatever you want here
// you can unsubscribe from the listener here or do it in
//componentwillunmount()
this.Homelistener = null; // --> just set it to null
} else {
//do something
SplashScreen.hide();
}
});
关于Firebase onAuthStateChanged unsubscribe is not a function 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48230707/