我正在尝试将数据库连接到我的App.js,它可以工作,但是当我尝试进行身份验证时,会出现以下错误:
错误:您试图通过调用firebase.app()使用未安装在Android项目中的firebase模块。 确保您拥有:
这是我在身份验证(运行)之前的App.js
import * as firebase from 'firebase';
import 'firebase/firestore';
if (firebase.apps.length === 0){
firebase.initializeApp(firebaseConfig);
}
export default class App extends React.Component {
render() {
return (
<NavigationContainer>
<RestaurantNavigator />
</NavigationContainer>
)
}
}
这是我经过身份验证的App.js(无法正常工作,请参见之前的错误)
import * as firebase from 'firebase';
import 'firebase/firestore';
import auth from '@react-native-firebase/auth';
if (firebase.apps.length === 0){
firebase.initializeApp(firebaseConfig);
}
function LoginApp(){
const [initializing, setInitializing] = useState(true);
const [user, setUser] = useState();
function onAuthStateChanged(user){
setUser(user);
if (initializing) setInitializing(false);
}
useEffect(() => {
const subscriber = auth().onAuthStateChanged(onAuthStateChanged);
return subscriber; // unsubscribe on unmount
}, []);
if (initializing) return null;
if (!user) {
return (
<View>
<Text>Login</Text>
</View>
);
}
}
export default class App extends Component {
render() {
return (
<View style = {styles.container}>
<LoginApp />
<NavigationContainer>
<RestaurantNavigator />
</NavigationContainer>
</View>
)
}
}
我做错了什么?