【发布时间】:2019-11-01 17:28:34
【问题描述】:
我正在制作一个 react-native 应用程序,我想知道连接到 Firebase 的最佳方式是什么?我在https://rnfirebase.io/ 找到了一些资源,对我来说它看起来不错,但我想我会问一下,以防有人感觉不同。另外,这可以在带有 expo 的设备上运行我的应用程序吗?谢谢!!
【问题讨论】:
标签: firebase react-native expo
我正在制作一个 react-native 应用程序,我想知道连接到 Firebase 的最佳方式是什么?我在https://rnfirebase.io/ 找到了一些资源,对我来说它看起来不错,但我想我会问一下,以防有人感觉不同。另外,这可以在带有 expo 的设备上运行我的应用程序吗?谢谢!!
【问题讨论】:
标签: firebase react-native expo
如果你想使用firebase,你必须分离Expo。
这些 Unimodules 设计用于分离的 ExpoKit 和 vanilla React Native。每个模块都有详细的安装说明,请参考React Native Firebase docs on more detailed usage
expo ejectyarn add expo-firebase-app用法
import firebase from 'expo-firebase-app';
import 'expo-firebase-database';
firebase.database()
import firebase from 'expo-firebase-app';
// Initialize Firebase
const firebaseConfig = {
apiKey: "<YOUR-API-KEY>",
authDomain: "<YOUR-AUTH-DOMAIN>",
databaseURL: "<YOUR-DATABASE-URL>",
storageBucket: "<YOUR-STORAGE-BUCKET>"
};
firebase.initializeApp(firebaseConfig);
import React from 'react';
import { View, Text } from 'react-native';
import firebase from 'expo-firebase-app';
class App extends React.Component {
constructor() {
super();
this.state = {
isAuthenticated: false,
};
}
componentDidMount() {
firebase.auth().signInAnonymously()
.then(() => {
this.setState({
isAuthenticated: true,
});
});
}
render() {
// If the user has not authenticated
if (!this.state.isAuthenticated) {
return null;
}
return (
<View>
<Text>Welcome to my awesome app!</Text>
</View>
);
}
}
export default App;
【讨论】: