TypeError:销毁不是函数NextJS

发布于 2025-01-30 09:16:17 字数 274 浏览 1 评论 0原文

当我将NextJS应用程序从9升级到12时。显示了一些错误,这些错误在以前的版本中没有得到照顾。其中之一是:typeError:destion destion不是函数

在控制台中,我可以看到它提到的next-dev.js?3515:25警告:使用效应不得返回函数以外的任何内容,用于清理。您返回了null。如果您的效果不需要清理,请返回未定义(或者没有任何

不确定是因为NextJ的更新在检查过程中变得过于严格,但是我会为自己和每个人都放下解决方案。

When I upgraded nextjs application from 9 to 12. There were some errors shown, that were not being taken take care of in previous version. One of them was: typeError: destroy is not a function

In the console I could see it mentioned next-dev.js?3515:25 Warning: useEffect must not return anything besides a function, which is used for clean-up. You returned null. If your effect does not require clean up, return undefined (or nothing

Not sure it was because of the update nextjs has become too strict during it's checking, but I will put it down the solution for myself and everyone.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

走走停停 2025-02-06 09:16:17

在几乎所有情况下,当您尝试从使用效果挂钩中返回任何功能的任何内容时,都会发生此错误。

错误

useEffect(() => someFunction());

useEffect(() => {
   return someFunction();
});

修复程序

useEffect(() => {
   someFunction();
});

有关更多信息,请阅读以下文章,

https://typeofnan.dev/fix-uncaught-typeerror-destroy-is-is-not-a-not-a-function-in-react/

In almost all of the cases this error occurs when you tried to return anything from your useEffect hook that is not a function.

The fault,

useEffect(() => someFunction());

or

useEffect(() => {
   return someFunction();
});

The Fix,

useEffect(() => {
   someFunction();
});

For more information read the following article,

https://typeofnan.dev/fix-uncaught-typeerror-destroy-is-not-a-function-in-react/

一影成城 2025-02-06 09:16:17

我也遇到了同样的问题,我的下一个应用程序从V9升级到V12。我之所以找到它,是因为

以前的使用效果像(我的下一个v9)=

  useEffect(() => {
    return () => {
      removeEventListener("blur", updateWarning);

      const inputFile = document.getElementById("input-file-ujian");
      if (inputFile) {
        inputFile.removeEventListener("click", (e) => {
          window.removeEventListener("blur", updateWarning);
        });
        inputFile.removeEventListener("change", handleChange);
      }

      const videos = document.getElementsByClassName("note-video-clip");
      for (let i = 0; i < videos.length; i++) {
        videos[i].removeEventListener("mouseleave", () => {
          window.addEventListener("blur", updateWarning);
        });
        videos[i].removeEventListener("mouseenter", () => {
          window.removeEventListener("blur", updateWarning);
        });
      }
    };
  }, [pesertaUjian, warning]);

这是我的下一个V12(我删除返回代码)=

useEffect(() => {
      removeEventListener("blur", updateWarning);

      const inputFile = document.getElementById("input-file-ujian");
      if (inputFile) {
        inputFile.removeEventListener("click", (e) => {
          window.removeEventListener("blur", updateWarning);
        });
        inputFile.removeEventListener("change", handleChange);
      }

      const videos = document.getElementsByClassName("note-video-clip");
      for (let i = 0; i < videos.length; i++) {
        videos[i].removeEventListener("mouseleave", () => {
          window.addEventListener("blur", updateWarning);
        });
        videos[i].removeEventListener("mouseenter", () => {
          window.removeEventListener("blur", updateWarning);
        });
      }
  }, [pesertaUjian, warning]);

我不知道为什么,我只是在使用效果中删除我的所有返回代码,这对我来说是工作的

更新:
更新,我发现,如果您使用使用效果和异步,请等待。不要喜欢它

useEffect(async() => {},[])

但是您可以在使用效果之外创建函数异步等待,例如

const yourFunction = async () => {}

useEffect(() => yourFunction(),[])

I also got the same issue, i was upgraded my Next App from v9 to v12. And i found it because the useEffect

My code before was like (my Next v9) =

  useEffect(() => {
    return () => {
      removeEventListener("blur", updateWarning);

      const inputFile = document.getElementById("input-file-ujian");
      if (inputFile) {
        inputFile.removeEventListener("click", (e) => {
          window.removeEventListener("blur", updateWarning);
        });
        inputFile.removeEventListener("change", handleChange);
      }

      const videos = document.getElementsByClassName("note-video-clip");
      for (let i = 0; i < videos.length; i++) {
        videos[i].removeEventListener("mouseleave", () => {
          window.addEventListener("blur", updateWarning);
        });
        videos[i].removeEventListener("mouseenter", () => {
          window.removeEventListener("blur", updateWarning);
        });
      }
    };
  }, [pesertaUjian, warning]);

and this is my Next v12 (I remove the return code) =

useEffect(() => {
      removeEventListener("blur", updateWarning);

      const inputFile = document.getElementById("input-file-ujian");
      if (inputFile) {
        inputFile.removeEventListener("click", (e) => {
          window.removeEventListener("blur", updateWarning);
        });
        inputFile.removeEventListener("change", handleChange);
      }

      const videos = document.getElementsByClassName("note-video-clip");
      for (let i = 0; i < videos.length; i++) {
        videos[i].removeEventListener("mouseleave", () => {
          window.addEventListener("blur", updateWarning);
        });
        videos[i].removeEventListener("mouseenter", () => {
          window.removeEventListener("blur", updateWarning);
        });
      }
  }, [pesertaUjian, warning]);

I don't know why, I just remove all my return code in my useEffect and it's work for me

Update:
Update, i found that if you are using useEffect and async await. Don't use like it

useEffect(async() => {},[])

but you can create function async await outside the useEffect, for example

const yourFunction = async () => {}

useEffect(() => yourFunction(),[])

魂ガ小子 2025-02-06 09:16:17

我要维护的代码中有很多位置,例如使用null喜欢:

useEffect(() => {
   if (variantSelected) {
     const productViewTrackingTimeout = setTimeout(
       useProductViewTracking({
        ...blah blah
       }),
       1000
     );
     return () => {
       clearTimeout(productViewTrackingTimeout);
     };
   }
   return null;
 }, [variantSelected, productTitle, router]);```

I removed all return null values, and just putting a return works too. But not any value.

There were a lot of place in the code which I am maintining where useEffect was returning null like:

useEffect(() => {
   if (variantSelected) {
     const productViewTrackingTimeout = setTimeout(
       useProductViewTracking({
        ...blah blah
       }),
       1000
     );
     return () => {
       clearTimeout(productViewTrackingTimeout);
     };
   }
   return null;
 }, [variantSelected, productTitle, router]);```

I removed all return null values, and just putting a return works too. But not any value.
时光清浅 2025-02-06 09:16:17

从React 17更新为18时,我会遇到相同的错误,但是就我而言,这是由usefect在异步中引起的。删除异步/等待,然后切换到


代码失败的代码

useEffect(async () => {
  const result = await fetchData()
  // Do stuff
})

有效的

useEffect(() => {
  fetchData().then(result => {
    // Do stuff
  })
})

I got this same error when updating from react 17 to 18, but in my case it was caused by the function in a useEffect being async. Removing async/await and switching to .then(result => {...} fixed it for me.

Eks:
Code that failed

useEffect(async () => {
  const result = await fetchData()
  // Do stuff
})

Code that works

useEffect(() => {
  fetchData().then(result => {
    // Do stuff
  })
})
乖乖公主 2025-02-06 09:16:17

这是因为异步回调不能用于使用效率。让我们了解无法直接从useFeff()挂钩直接调用异步回调函数的原因。这是因为使用效果挂钩期望其效应功能返回清理功能或根本没有

It is because async callbacks cannot be used in useEffect. Let's understand the reasons why an asynchronous callback function cannot be called directly from a useEffect() hook. This is because the useEffect hook expects its effect function to return either a cleanup function or nothing at all

终陌 2025-02-06 09:16:17

对我来说,错误是我制作了useffect函数差异异步

React.useEffect(async ()=>{
 const data= await getData(`GeneralSales/OrderNumber/${orderNumber.orderNumber}`)
 setOrder(data)
},[])

For me, the error was that I made the useEffect function asynchronous

React.useEffect(async ()=>{
 const data= await getData(`GeneralSales/OrderNumber/${orderNumber.orderNumber}`)
 setOrder(data)
},[])
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文