忽略 React 中的某些控制台错误/警告?

Ignore certain console errors / warnings in React?

我的应用程序中出现了大量这样的控制台错误:

Warning: React does not recognize the textStyle prop on a DOM element. If you intentionally want it to appear in the DOM as a custom attribute, spell it as lowercase textstyle instead. If you accidentally passed it from a parent component, remove it from the DOM element.

理想情况下,我会修复错误,但遗憾的是,这是不可能的,因为我正在使用带样式组件的样式系统: https://github.com/styled-system/styled-system/issues/1044

作为一个不太理想的解决方法,我想从 React 的开发版本的控制台中禁用某些错误。这能做到吗?

不确定这是否重要,但我正在使用 React Native Web。

您可以使用自己的函数覆盖 console.warn 方法,过滤掉您想要忽略的警告:

const backup = console.warn;

console.warn = function filterWarnings(msg) {
  const supressedWarnings = ['warning text', 'other warning text'];

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

console.warn('I\'ll appear as a warning');

console.warn('warning text - I will not');

我不确定 React 内部使用的是哪种控制台方法,因此您可能需要对 console.infoconsole.logconsole.error.

执行相同的操作

你也可以只使用 react 的生产版本,默认情况下它会抑制所有警告,但当然你不能挑选,那样的话你会失去所有警告。