简体   繁体   English

如何隐藏 componentWillMount 警告

[英]How to hide componentWillMount warnings

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.这些警告来自我的理解,不是因为我做错了什么,而是因为 react-router-dom 和 react-select 使用了已弃用的 componentWillMount 函数。 How do I get rid of the warnings?我如何摆脱警告?

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 .我曾尝试在此站点上查找问题,但最接近我找到的解决方案是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?从那时起,这些大/流行的 npm 包还没有更新吗?

This is one of the warnings I get (the others are similar):这是我收到的警告之一(其他类似):

react-dom.development.js:11494 Warning: componentWillMount has been renamed, and is not recommended for use. react-dom.development.js:11494 警告:componentWillMount 已重命名,不推荐使用。 See https:// fb .见 https:// fb 。 me/react-async-component-lifecycle-hooks for details. me/react-async-component-lifecycle-hooks 了解详情。

  • Move code with side effects to componentDidMount, and set initial state in the constructor.将有副作用的代码移动到 componentDidMount,并在构造函数中设置初始状态。
  • Rename componentWillMount to UNSAFE_componentWillMount to suppress this warning in non-strict mode.将 componentWillMount 重命名为 UNSAFE_componentWillMount 以在非严格模式下抑制此警告。 In React 17.x, only the UNSAFE_ name will work.在 React 17.x 中,只有 UNSAFE_ 名称有效。 To rename all deprecated lifecycles to their new names, you can run npx react-codemod rename-unsafe-lifecycles in your project source folder.要将所有已弃用的生命周期重命名为其新名称,您可以在项目源文件夹中运行npx react-codemod rename-unsafe-lifecycles

Please update the following components: BrowserRouter, Route, Router, Switch请更新以下组件:BrowserRouter、Route、Router、Switch

(I actually tried to run "npx react-codemod rename-unsafe-lifecycles" but it made no difference) (我实际上尝试运行“npx react-codemod rename-unsafe-lifecycles”,但没有任何区别)

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.我无法控制这些 npm 包在内部如何工作,因此我发现经常收到这些无法修复或删除的警告令人沮丧。

The common way to fix this would be to update the affected libraries (as you say react-router and react-select).解决此问题的常用方法是更新受影响的库(如您所说的 react-router 和 react-select)。 If these are being maintained, then they would release new versions that don't produce these warnings.如果这些正在维护,那么他们将发布不会产生这些警告的新版本。 If that is not an option for you, then I don't know, I don't think React has a way of suppressing specific warnings.如果这不是您的选择,那么我不知道,我认为 React 没有抑制特定警告的方法。

Note that the warnings are only shown in the dev build of React, they won't be shown in the production build of React (see DOCs ).请注意,警告仅显示在 React 的开发版本中,它们不会显示在 React 的生产版本中(请参阅DOC )。

The best I found..我找到的最好的..

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;

使用下面的代码:

console.disableYellowBox = true;

JeanMGirard's answer causes Uncaught RangeError: Maximum call stack size exceeded in some instances like when you forget to add dependencies to the dependency array within the useEffect React Hook. JeanMGirard 的回答会导致Uncaught RangeError: Maximum call stack size exceeded在某些情况下,例如当您忘记将依赖项添加到useEffect React Hook 中的依赖项数组时。 This can make it very hard to debug certain bugs in your code.这会使调试代码中的某些错误变得非常困难。

Normally, React DevTools would handle this by displaying the warning cause and possible solutions.通常,React DevTools 会通过显示警告原因和可能的解决方案来处理这个问题。

Here's a solution which ensures React DevTools handles our warnings as normal, but hides the UNSAFE_ warnings:这是一个解决方案,可确保 React DevTools 正常处理我们的警告,但隐藏UNSAFE_警告:

const backup = console.warn;

console.warn = function filterWarnings(warning) {
    // If the warning includes any of the following text, let's hide it.
    const supressedWarnings = [
        "Warning: componentWillMount has been renamed, and is not recommended for use.",
        "Warning: componentWillReceiveProps has been renamed, and is not recommended for use.",
        "Warning: componentWillUpdate has been renamed, and is not recommended for use.",
    ];

    if (warning.length && !supressedWarnings.some(entry => warning.includes(entry))) {
        backup.apply(console, arguments);
    }
};

In index.android.js, you can use:在 index.android.js 中,您可以使用:

  console.disableYellowBox = true
  console.warn = () => {}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM