简体   繁体   English

反应:清理 function 从未调用

[英]React: Clean-Up function never called

I'm building a react app that uses Firebase and Firestore.我正在构建一个使用 Firebase 和 Firestore 的反应应用程序。

I'm using the onSnapshot function so I get the real time data from Firestore, but I was wondering how I can remove that listener.我正在使用onSnapshot function 所以我从 Firestore 获取实时数据,但我想知道如何删除该侦听器。

Yeah I know, I must use the cleanup function of the useEffect hook, but I can't make it work, here's my code:是的,我知道,我必须使用 useEffect 挂钩的清理 function ,但我无法让它工作,这是我的代码:

useEffect(() => {
    let removeListener = getCanzoni(utente).onSnapshot(function (querySnapshot) {
      let promises = querySnapshot.docs.map(async function (doc) {
        let canzone = doc.data();
        canzone.id = doc.id;

        return canzone;
      });

      Promise.all(promises).then((canzoni) => {
        cambiaCanzoni(canzoni);
      });

      return function cleanup() {
        console.log("Removed Listener");
        removeListener();
      };
    });
  }, []);

The getCanzoni function is imported from another files and it's definition is: getCanzoni function 是从另一个文件导入的,它的定义是:

export function getCanzoni(utente) {
  return firestore
    .collection("Canzoni")
    .where("utente", "==", utente.uid)
    .orderBy("data", "desc");
}

When I remove the component, I don't see the 'Removed Listener' in the console.当我删除组件时,我在控制台中看不到“已删除侦听器”。 I know that the clean-up function is called when the dependency array changes or when the components is unmounted.我知道当依赖数组更改或卸载组件时会调用清理 function 。

Any idea or tips?任何想法或提示?

I've found the error:我发现了错误:

The clean up function must be defined in the hook's function body, not inside other function, like this:清理 function 必须在钩子的 function 主体中定义,而不是在其他 function 中定义,如下所示:

  useEffect(() => {
    let removeListener = getCanzoni(utente).onSnapshot(function (querySnapshot) {
      let promises = querySnapshot.docs.map(async function (doc) {
        let canzone = doc.data();
        canzone.id = doc.id;

        return canzone;
      });

      Promise.all(promises).then((canzoni) => {
        cambiaCanzoni(canzoni);
      });
    });

    return function cleanup() {
      console.log("Tolto il listener");
      removeListener();
    };
  }, []);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 清除不再引用的元素,并且从未添加到文档中 - Clean-up of elements that are no longer referenced, and were never added to the document React Native useEffect - 警告:除了用于清理的函数之外,效果函数不得返回任何内容 - React Native useEffect - Warning: An effect function must not return anything besides a function, which is used for clean-up “箭头 function 预期没有返回值”在 useEffect 中清理 function - "arrow function expected no return value" with clean-up function in useEffect 实体事件订阅清理 - Entity event subscriptions clean-up 什么是使用影响反应挂钩中的执行顺序及其内部清理逻辑? - What's useEffect execution order and its internal clean-up logic in react hooks? 我应该清理 useEffect 中的 handlePress 变量吗? - Should I clean-up handlePress variables in useEffect? 在删除对象之前运行Javascript清理代码? - Run Javascript clean-up code before object deletion? 在禁用的道具上反应组件函数从未调用过 - React component function on disabled prop never called 我已经阅读了关于Hooks的React Docs,我很困惑。 什么时候调用useEffect Hook清理函数? - I have read the React Docs on Hooks and I'm confused. When is the useEffect Hook clean up function called? RxJS:如何在传递下一个有效值之前进行一些清理? - RxJS: How to do some clean-up before the next valid value is passed?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM