gpt4 book ai didi

javascript - 如何隐藏 componentWillMount 警告

转载 作者:行者123 更新时间:2023-12-03 13:16:54 24 4
gpt4 key购买 nike

我收到四个无法在控制台中最小化的重大警告。这些警告来 self 的理解,不是因为我做错了什么,而是因为react-router-dom和react-select使用了已弃用的componentWillMount函数。如何消除警告?

我曾尝试在此网站上查找问题,但最接近我找到的解决方案是 https://stackoverflow.com/a/49166225/12057512 。由于答案是一年多前的,我想知道情况是否仍然如此。从那以后这些大/流行的 npm 包还没有更新吗?

这是我收到的警告之一(其他类似):

react-dom.development.js:11494 Warning: componentWillMount has been renamed, and is not recommended for use. See https:// fb . me/react-async-component-lifecycle-hooks for details.

  • Move code with side effects to componentDidMount, and set initial state in the constructor.
  • Rename componentWillMount to UNSAFE_componentWillMount to suppress this warning in non-strict mode. In React 17.x, only the UNSAFE_ name will work. To rename all deprecated lifecycles to their new names, you can run npx react-codemod rename-unsafe-lifecycles in your project source folder.

Please update the following components: BrowserRouter, Route, Router, Switch

(我实际上尝试运行“npx react-codemod rename-unsafe-lifecycles”,但没有什么区别)

我无法控制这些 npm 包的内部工作方式,因此我感到沮丧的是,我不断收到这些无法修复或删除的警告。

最佳答案

我发现的最好的..

const warn = console.warn;

function logWarning(...warnings){
let showWarning = true;
warnings.forEach(warning => {
if (warning.includes("UNSAFE_")) showWarning = false;
else if (warning.includes("SourceMap")) showWarning = false;
else if (warning.includes("DevTools")) showWarning = false;
});
if(showWarning) warn(...warnings);
}


console.warn = logWarning;

关于javascript - 如何隐藏 componentWillMount 警告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57904356/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com