我刚刚升级了我的React Native,现在iOS模拟器有一堆警告。除了修复它们之外,我如何隐藏这些警告以便我可以看到下面的内容?
根据React Native Documentation,您可以通过将disableYellowBox
设置为true
来隐藏警告消息,如下所示:
console.disableYellowBox = true;
(但不适用于您自己的代码)
原因:当初始化一个新的RN应用程序时,Xcode项目包含更接近100个警告,这会分散噪音(但可能无害)
解决方案:在相关目标的“构建设置”下,将所有警告设置为“是”。
选择性隐藏某些警告(在升级到最新且最好的RN版本后无限期显示)的更好方法是在项目中的常用JS文件中设置console.ignoredYellowBox。例如,在今天将我的项目升级到RN 0.25.1后,我看到很多......
警告:不推荐使用ReactNative.createElement ...
我仍然希望能够看到来自React-Native的有用警告和错误消息,但我想压缩这个特殊的警告,因为它来自一个尚未纳入RN 0.25中的重大变化的外部npm库。所以在我的App.js中我添加了这一行......
// RN >= 0.52
import {YellowBox} from 'react-native';
YellowBox.ignoreWarnings(['Warning: ReactNative.createElement']);
// RN < 0.52
console.ignoredYellowBox = ['Warning: ReactNative.createElement'];
这样我仍然会得到其他错误和警告对我的开发环境有用,但我不再看到那个特定的错误。
要禁用黄色框的位置
console.disableYellowBox = true;
在您的应用程序中的任通常在根文件中,因此它将适用于iOS和Android。
例如
export default class App extends React.Component {
render() {
console.disableYellowBox = true;
return (<View></View>);
}
}
在index.js文件中添加以下代码
console.disableYellowBox = true;
import {AppRegistry} from 'react-native';
import App from './App';
import {name as appName} from './app.json';
console.disableYellowBox = true;
AppRegistry.registerComponent(appName, () => App);
如果您想在特定版本中隐藏它们,因为您正在进行演示或其他操作,您可以编辑Xcode方案以使其成为版本构建,并且这些黄色警告将不会显示。此外,您的应用程序将运行得更快。
您可以通过执行以下操作为您的模拟器和真实设备编辑Scheme:
Product
> Scheme
> Edit Scheme...
Build Configuration
从Debug
改为Release
。对于那些试图从控制台禁用红色警告的人来说,这给了绝对无用的信息,从feb / 17开始,你可以在某处添加这行代码
console.error = (error) => error.apply;
禁用所有console.error
要禁用黄色框,请在应用程序的任何位置放置console.disableYellowBox = true;
。通常在根文件中,因此它将适用于iOS和Android。
如需了解更多详情,请查看official document
console.disableYellowBox = true;
这适用于应用程序级别将它放在index.js文件中的任何位置
在AppDelegate.m文件中,您可以更改此行:
jsCodeLocation = [NSURL URLWithString:@"http://localhost:8081/index.ios.bundle?platform=ios&dev=true"];
并在最后用dev=true
取代dev=false
。