简体   繁体   中英

Is there any way to disable all the errors and warnings that appear in the console?

I don't want to show the warnings in the console for a certain development environment. Is there any way to achieve that? My application was bootstrapped using create react app.

Since you are using React, my guess is you are already using babel. There's a plugin for that purpose. It's called babel-plugin-transform-remove-console . This will exclude all console.log 's statement during the build process.
Install that in your app and configure it via .babelrc as follows:

{
  "plugins": ["transform-remove-console"]
}

You can also specify the variant(s) of the console functions to exclude:

{
  "plugins": [ ["transform-remove-console", { "exclude": [ "error", "warn"] }] ]
}

My advise is to not use console logs in your code except necessary.

In my App.js I have the following code for accomplishing this:

import { YellowBox } from 'react-native';

componentDidMount() {
    // The following lines are a workaround
    // in order to stop getting warnings about timer
    // See: https://github.com/firebase/firebase-js-sdk/issues/97#issuecomment-365456531
    YellowBox.ignoreWarnings(['Setting a timer']);
    const _console = _.clone(console);
    console.warn = message => {
      if (message.indexOf('Setting a timer') <= -1) {
        _console.warn(message);
      }
    };
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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