[英]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.
我的应用程序是使用create react app引导的。
Since you are using React, my guess is you are already using babel. 既然你正在使用React,我猜你已经在使用了babel。 There's a plugin for that purpose.
有一个插件用于此目的。 It's called babel-plugin-transform-remove-console .
它被称为babel-plugin-transform-remove-console 。 This will exclude all
console.log
's statement during the build process. 这将在构建过程中排除所有
console.log
的语句。
Install that in your app and configure it via .babelrc
as follows: 在您的应用程序中安装它并通过
.babelrc
配置,如下所示:
{
"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: 在我的App.js中,我有以下代码来完成此任务:
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);
}
};
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.