不知道为啥,我自己搭建的umi项目用着用着就多了一些报错和警告
有如下几种:
- 'react-error-overlay is not meant for use in production. You should ensure it is not included in your build to reduce bundle size.'
- 'Warning: componentWillReceiveProps has been renamed'
- 'Warning: componentWillUpdate has been renamed'
-
Warning: Please use \
require("history").createHashHistory` instead of `require("history/createHashHistory")`. Support for the latter will be removed in the next major release.`, - 'ui socket init',
首先说下第二个和第三个,我用函数式组件写的呀,自己的文件压根就没有componentWillReceiveProps
和componentWillUpdate
再说第4个,我搜索了半天,大多数解法竟然是去node_modules
改代码...
为什么要干掉呢? 闹心加影响判断,每天面对着这些无价值的信息,多多少少会浪费一些时间和心情。
就在我不知道该怎么处理好的时候,一篇文章提醒了我,可以用console.warn = ()=>{}
来解决
当然,上面的方式过于武断了,于是乎,我在document.ejs
中加上如下代码,确保第一个被执行
<script>
(() => {
//过滤掉一些无用的警告、没有价值的报错
//代理console.warn方法
const _consoleWarn = console.warn;
console.warn = (...rest) => {
if (
![
'react-error-overlay is not meant for use in production. You should ensure it is not included in your build to reduce bundle size.',
'Warning: componentWillReceiveProps has been renamed',
'Warning: componentWillUpdate has been renamed',
].some((item) => rest[0].indexOf(item) !== -1)
) {
_consoleWarn(...rest);
}
};
//代理console.error
const _consoleError = console.error;
console.error = (...rest) => {
if (
![
`Warning: Please use \`require("history").createHashHistory\` instead of \`require("history/createHashHistory")\`. Support for the latter will be removed in the next major release.`,
'ui socket init',
].includes(rest[0].toString())
) {
_consoleError(...rest);
}
};
})();
</script>
第一呢,不会影响console.warn
和console.error
的使用方式
第二呢,确认过不需要处理的报错和警告再进行屏蔽
自己练习的项目不需要考虑部署构建的问题,所以这点代码加在这也没啥影响,但是现在有了一个干干净净的控制台,舒服了!
第五个只要关闭Umi UI就好了。
网友评论