Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ignore certain console errors / warnings in React?

I have a huge number of console errors like this appearing in my app:

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.

Ideally I would fix the errors but sadly that's not possible as I'm using Styled System with Styled Components: https://github.com/styled-system/styled-system/issues/1044

As a less than ideal workaround Id like to disable certain errors from the console for the development version of React. Can this be done?

Not sure if it matters but I'm using React Native Web.

like image 934
Evanss Avatar asked Oct 01 '20 15:10

Evanss


People also ask

How do I ignore a warning in react?

The Yellow warning box in React Native can be both helpful and annoying. There are many errors that can be ignored but end up blocking necessary pieces of the application. To disable the yellow box place console. disableYellowBox = true; anywhere in your application.

How do I hide console errors?

console. clear(); is good option to hide the console error because some time on running site we don't want to show error so we hide them in PHP.


1 Answers

You can override the console.warn method with your own function that filters out the warnings you want to ignore:

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');

I'm not sure which console method react is using internally, so you may need to do the same for console.info, console.log and console.error.

You can also just use the production version of react, which suppresses all warnings by default, but of course you can't pick and choose, you loose all warnings in that case.

like image 122
D-Money Avatar answered Oct 18 '22 10:10

D-Money