如何隐藏componentWillMount警告 [英] How to hide componentWillMount warnings
问题描述
我收到四个不能在控制台中最小化的大警告.这些警告来自我的理解,不是因为我做错了什么,而是因为react-router-dom和react-select使用了不赞成使用的componentWillMount函数.如何摆脱警告?
I'm getting four big warnings that can not be minimized in my console. These warnings are from what I understand not because I have done anything wrong, but because react-router-dom and react-select use the deprecated componentWillMount function. How do I get rid of the warnings?
我已经尝试在此站点上查找问题,但是最接近我发现的解决方案的是 https://stackoverflow.com/a/49166225/12057512 .由于答案是一年多以前的,所以我想知道是否仍然如此.从那时起,这些大型/受欢迎的npm软件包是否仍未更新?
I have tried looking up the problem on this site, but the closest to a solution I have found is https://stackoverflow.com/a/49166225/12057512. Since the answer is from over a year ago I am wondering if this is still the case. Have these big/popular npm packages still not updated since then?
这是我收到的警告之一(其他类似):
This is one of the warnings I get (the others are similar):
react-dom.development.js:11494警告:componentWillMount已被执行重命名,不建议使用.参见https://fb.我/react-async-component-lifecycle-hooks了解详情.
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.
- 将具有副作用的代码移动到componentDidMount,并在构造函数中设置初始状态.
- 将componentWillMount重命名为UNSAFE_componentWillMount以在非严格模式下禁止显示此警告.在React 17.x中,只有UNSAFE_名称将工作.要将所有已弃用的生命周期重命名为其新名称,您可以可以在您的项目中运行
npx react-codemod named-unsafe-lifecycles
源文件夹.
请更新以下组件:BrowserRouter,Route,Router,切换
Please update the following components: BrowserRouter, Route, Router, Switch
(我实际上试图运行"npx react-codemodname-unsafe-lifecycles",但没有区别)
(I actually tried to run "npx react-codemod rename-unsafe-lifecycles" but it made no difference)
我无法控制这些npm软件包在内部的工作方式,因此感到沮丧的是,我不断收到这些无法修复或删除的警告.
I have no control over how these npm packages work internally, so I find it frustrating that I constantly get these warnings that I can not fix or remove.
推荐答案
我找到的最好的..
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;
这篇关于如何隐藏componentWillMount警告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!