firebase - 使用 useFirestoreConnect 仅加载一次数据
问题描述
我正在使用 useFirestoreConnect 挂钩从集合中获取数据,并且只需要一次来自集合的数据。
但是在这里的官方文档中,没有提到关闭监听器。由于我的每日免费配额超过了某个时间。
是否可以使用 useFirestoreConnect 只加载一次数据?
解决方案
选项 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#get_a_document
推荐阅读
- ios - 禁用 UILabel、UIText 等的自动调整字体。在 iOS/Swift 中?
- windows - 批处理文件中的 >nul 不会静音
- azure - 一天后无法从直接 API 获取活动
- angular - Angular 为什么构建的应用程序会出现无法启用 prod 模式的错误?
- multithreading - 等待线程队列为空
- kubernetes - 调用 Kubernetes 服务失败
- android - 为数据绑定中的编辑文本设置选择
- c - Clang:编译 IR、C 文件并在一行中应用 opt
- excel - 通过键入而不是选择来验证 Excel 数据
- sql-insert - mysql使用特殊的自动增量添加数据