Javascript 为什么firebase无法识别我的firebase实例&引用;调用Firebase App.intializeApp();
我有两个文件,一个是Javascript 为什么firebase无法识别我的firebase实例&引用;调用Firebase App.intializeApp();,javascript,firebase,react-native,firebase-authentication,react-navigation,Javascript,Firebase,React Native,Firebase Authentication,React Navigation,我有两个文件,一个是App.js,我在其中设置我的React Navigation StackNavigator以及我的firebase实例,另一个是Login.js,我在其中登录用户 我试图检测用户是否已经登录(通过firebase持久登录),但由于某种原因,login.js的componentDidMount()中没有检测到我的firebase实例。这对我来说毫无意义,因为在我的Login.js的其他地方,我在firebase实例上调用fxns,就像使用Google登录一样 我应该如何安排f
App.js
,我在其中设置我的React Navigation StackNavigator以及我的firebase实例,另一个是Login.js
,我在其中登录用户
我试图检测用户是否已经登录(通过firebase持久登录),但由于某种原因,login.js
的componentDidMount()
中没有检测到我的firebase实例。这对我来说毫无意义,因为在我的Login.js
的其他地方,我在firebase实例上调用fxns,就像使用Google登录一样
我应该如何安排firebase初始化/auth检查,以确保firebase实例与持久性auth检查一起被正确检测到
我已经看过了,但那个人似乎有一个不同的问题,因为他们只关心单个文件中的firebase实例
App.js的相关部分
import { StackNavigator } from 'react-navigation';
import * as firebase from 'firebase';
import 'firebase/firestore';
import Login from './components/login';
const Application = StackNavigator({
Login: { screen: Login,
navigationOptions: {
title: 'Login',
}
},
SignUp: { screen: SignUp,
navigationOptions: {
title: 'Signup',
}
},
Home: { screen: Home,
navigationOptions: {
title: 'Home'
}
},
});
const firebaseConfig = {
apiKey: "APIKEY",
authDomain: "AUTH_DOMAIN",
databaseURL: "DATABASE_URL",
projectId: "PROJECT_ID",
storageBucket: "BUCKET",
}
export default class App extends React.Component {
componentDidMount(){
//if no instance exists, initialize one
if (!firebase.apps.length) {
firebase.initializeApp(firebaseConfig);
}
}
render() {
return (
<Application/>
);
}
}
import * as firebase from 'firebase';
import Home from './home';
export default class Login extends React.Component {
constructor(props){
super(props)
this.state = {
isLoggingIn: true,
authFlag: false,
}
}
componentDidMount() {
//error is on the following line
firebase.auth().onAuthStateChanged((user) => {
if(!this.state.authFlag){
this.setState({ authFlag: true })
//persistent success, take user to Home
if(user){
this.props.navigation.navigate('Home');
return;
}
//user hasn't logged in before, must log in
else{
this.setState({isLoggingIn: false});
return;
}
}
});
}
}
正如我在上面的评论中提到的,子组件在其父组件之前安装<登录组件的code>componentDidMount
发生在应用程序的componentDidMount
之前
只需进行快速搜索,它看起来就像大多数人在他们的组件之外进行
firebase.initializeApp
子组件在其父组件之前装入<在应用程序的componentDidMount
之前发生登录组件的code>componentDidMount。。。。。我懂了。我很难理解它如何与React Native中的父->子数据流兼容。如果子组件在其父组件之前装入,这是否意味着不拒绝在父组件中初始化的数据流到其子组件的能力(如我的情况)?这取决于您如何获取数据。如果在componentDidMount
中提取数据,则应在父级中执行。然后,父级可以将数据向下传输到其子级,子级将在组件willreceiveprops
中接收数据。至少在React 17之前,也就是说……假设有一个导航器具有App.js
->Login.js
->Home.js
,这意味着可以在Home.js
中初始化firebase实例??如何重构我发布的代码,在App.js
中对其进行初始化,并允许Login.js
的componentDidMount()
访问它?哇,这是您最后链接的一个很好的指南,非常感谢!