简体   繁体   English

我在制作新的反应时遇到错误 - 本机应用程序

[英]I am getting an error when i was making a new react - native app

Uncaught TypeError: firebase_compat_app__WEBPACK_IMPORTED_MODULE_4__.initializeApp is not a function未捕获的类型错误:firebase_compat_app__WEBPACK_IMPORTED_MODULE_4__.initializeApp 不是 function

This is a error which is coming when I am making a new react - native app这是我做出新反应时出现的错误 - 本机应用程序

CODE:代码:

import { ActivityIndicator, StyleSheet, View } from 'react-native'
import React from 'react';
import * as firebase from 'firebase/compat/app';
import 'firebase/compat/auth';
import 'firebase/compat/database';

class LoadingScreen extends React.Component {

    componentDidMount() { 
        this.checkIfLoggedIn();
    };


    checkIfLoggedIn = () => {
        firebase.auth().onAuthStateChanged(user => {
            if (user) {
                this.props.navigation.navigate('DashboardScreen')
            } else {
                this.props.navigation.navigate('LoginScreen')
            };
        });
    };


    render() {
        return (
            <View style={styles.container}>
                <ActivityIndicator size="large" />
            </View>
        )
    }
};

const styles = StyleSheet.create({
    container: {
        flex: 1,
        justifyContent: 'center',
        alignItems: 'center'
    }
});

export default LoadingScreen;

I see a few concerns/problems with the above code:我看到上述代码存在一些问题/问题:

  • use of compat is not encouraged;不鼓励使用compat Firebase explicitly stated that this is a stop-gap measure for people with large v8 code bases that need to move to v9 but want to stick with the v8 syntax -- and that the v8 syntax (and compat ) are to be deprecated in the very near future Firebase 明确指出,对于拥有大型 v8 代码库、需要迁移到 v9 但又想坚持使用 v8 语法的人来说,这是一种权宜之计——并且 v8 语法(和compat )将在非常时期被弃用不远的将来
  • though Class Components remain valid in React, as you build new code I recommend you consider moving to Function Components尽管 Class 组件在 React 中仍然有效,但当您构建新代码时,我建议您考虑迁移到 Function 组件
  • there does not seem to be any attempt in your code to shut off the onAuthStateChanged() listener;您的代码中似乎没有任何尝试关闭onAuthStateChanged()侦听器; this is a resource leak in your code这是您的代码中的资源泄漏
  • it is hard to tell why that particular error is occurring;很难说出为什么会发生这种特定错误; you say that you initialize Firebase in a different file so we can't see how it is initialized你说你在不同的文件中初始化 Firebase 所以我们看不到它是如何初始化的
  • the above code relies on the "default app" (your use of firebase.auth() without specifying the particular app ) -- without knowing how you initialized Firebase we can't tell if that is "doing the right thing"上面的代码依赖于“默认应用程序”(您使用firebase.auth()而没有指定特定的app ) - 不知道您如何初始化 Firebase 我们无法判断这是否是“做正确的事”
  • I would encourage you to NOT rely on a "default app" but to explicitly specify the app you are invoking the Authentication service from我鼓励您不要依赖“默认应用程序”,而是明确指定您从中调用身份验证服务的应用程序

BTW: I have moved to using the Context API for initializing Firebase and sharing its services to the rest of my app.顺便说一句:我已经开始使用上下文 API 来初始化 Firebase 并将其服务共享给我的应用程序的 rest。 You can see this pattern in the FirebaseProvider.js of the following starter project.您可以在以下启动项目的 FirebaseProvider.js 中看到此模式。 Feel free to use/copy as you see fit: https://github.com/gregfenton/react-and-user-profiles-with-firebase-auth-and-firestore随意使用/复制您认为合适的: https://github.com/gregfenton/react-and-user-profiles-with-firebase-auth-and-firestore

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 firebase.database 不是 function 为什么我在反应本机应用程序中收到此错误? - firebase.database is not a function why I am getting this error in react native app? 从firestore(firebase)获取数据时出现错误。 我正在使用本机反应 - I am getting an error while fetching data from firestore(firebase). I am using react native 当我在 AWS Amplify 上重新加载我的 React 应用程序时出现“访问被拒绝”错误 - Getting an "Access Denied" error when I reload my React app on AWS Amplify 为什么我在 iOS(颤振应用程序)中收到 Firebase AppCheck 错误 - Why am I getting a Firebase AppCheck error in iOS (Flutter app) 我在 React 中的 signInWithEmailAndPassword 中出现错误:Firebase: Error (auth/network-request-failed) - I am getting error in signInWithEmailAndPassword in React: Firebase: Error (auth/network-request-failed) 我收到多个错误:尝试在 aws s3 存储桶中上传文件时出现意外字段 - I am getting multer error : unexpected field when i was trying to upload files in aws s3 bucket 我收到错误 AttributeError: 'Auth' object has no attribute 'get_user' in Flask App - i am getting error AttributeError: 'Auth' object has no attribute 'get_user' in Flask App 如何在我的 React 本机应用程序中覆盖 AmplifyTheme - How can I override the AmplifyTheme in my react native app 在命令提示符下运行 aws configure 时出现错误 - I am getting an error while running aws configure in command prompt 为什么我没有从反应 redux state 获得更新的值 - Why I am not getting the updated value from react redux state
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM