【问题标题】:What is the best way of connecting a react-native app to firebase? [closed]将 react-native 应用程序连接到 firebase 的最佳方式是什么? [关闭]
【发布时间】:2019-11-01 17:28:34
【问题描述】:

我正在制作一个 react-native 应用程序,我想知道连接到 Firebase 的最佳方式是什么?我在https://rnfirebase.io/ 找到了一些资源,对我来说它看起来不错,但我想我会问一下,以防有人感觉不同。另外,这可以在带有 expo 的设备上运行我的应用程序吗?谢谢!!

【问题讨论】:

    标签: firebase react-native expo


    【解决方案1】:

    如果你想使用firebase,你必须分离Expo

    这些 Unimodules 设计用于分离的 ExpoKit 和 vanilla React Native。每个模块都有详细的安装说明,请参考React Native Firebase docs on more detailed usage

    1. expo eject
    2. yarn 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;
    

    【讨论】:

    • @Matt123 如果我的回答对您有帮助,请您检查并选择“V”和箭头吗?
    • 不需要expo弹出,firebase可以作为api端点使用。
    • @SuleymanSah 这不是世博推荐的使用方法。
    • 为什么不推荐?
    • @SuleymanSah 原因写在你回复的评论里。
    猜你喜欢
    • 2019-09-18
    • 1970-01-01
    • 2020-04-13
    • 2010-09-09
    • 1970-01-01
    • 2010-09-10
    • 2019-04-29
    • 2018-05-24
    相关资源
    最近更新 更多