首页 > 解决方案 > 使用 useFirestoreConnect 仅加载一次数据

问题描述

我正在使用 useFirestoreConnect 挂钩从集合中获取数据,并且只需要一次来自集合的数据。

但是在这里的官方文档中,没有提到关闭监听器。由于我的每日免费配额超过了某个时间。

是否可以使用 useFirestoreConnect 只加载一次数据?

标签: firebasereact-redux-firebaseredux-firestore

解决方案


选项 1:卸载组件以停止侦听器。

useFirestoreConnect 是一个 React 挂钩,可在组件安装和卸载时为您管理附加和分离侦听器。

https://react-redux-firebase.com/docs/firestore

选项 2:使用 useFirestore() 进行查询

const firestore = useFirestore();
var docRef = firestore.collection("cities").doc("SF");

docRef.get().then((doc) => {
     if (doc.exists) {
     console.log("Document data:", doc.data());
     } else {
          // doc.data() will be undefined in this case
          console.log("No such document!");
     }
}).catch((error) => {
    console.log("Error getting document:", error);
});

http://react-redux-firebase.com/docs/api/useFirestore.html

https://firebase.google.com/docs/firestore/query-data/get-data#g​​et_a_document


推荐阅读