如何将React Progressive Web App(PWA)链接到Firebase数据库?

时间:2019-06-10 12:35:00

标签: reactjs firebase firebase-realtime-database progressive-web-apps

到目前为止,我已经通过手动在Firebase数据库中进行更改并将其实时显示在我的PWA上,成功更新了我的数据。 但是现在我希望将数据写入/插入Firebase实时数据库中,并同时显示更改。

我的问题是,我们如何将数据从React WebApp推送到Firebase实时数据库?

1 个答案:

答案 0 :(得分:1)

看看this这些是官方文档。

您需要使用自己的应用程序数据启动应用程序

 var config = {
    apiKey: "apiKey",
    authDomain: "projectId.firebaseapp.com",
    databaseURL: "https://databaseName.firebaseio.com",
    storageBucket: "bucket.appspot.com"
  };
  firebase.initializeApp(config);

  // Get a reference to the database service
  var database = firebase.database();

然后您可以使用暴露的sdk方法实例化和操作

var database = firebase.database();
var starCountRef = firebase.database().ref('posts/' + postId + '/starCount');
starCountRef.on('value', function(snapshot) {
  updateStarCount(postElement, snapshot.val());
});

来源:官方文档