【问题标题】:Load data only once with useFirestoreConnect使用 useFirestoreConnect 仅加载一次数据
【发布时间】:2021-02-08 11:51:28
【问题描述】:

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

但是在官方文档here上,并没有提到关闭监听器。因此我的每日免费配额超过了某个时间。

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

【问题讨论】:

    标签: firebase react-redux-firebase redux-firestore


    【解决方案1】:

    选项 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

    【讨论】:

      猜你喜欢
      • 2021-12-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-12
      • 2018-10-24
      相关资源
      最近更新 更多