【发布时间】:2016-05-20 11:04:31
【问题描述】:
我刚刚升级了我的 React Native,现在 iOS 模拟器有一堆警告。除了修复它们,我如何隐藏这些警告以便我可以看到下面的内容?
【问题讨论】:
标签: ios react-native show-hide
我刚刚升级了我的 React Native,现在 iOS 模拟器有一堆警告。除了修复它们,我如何隐藏这些警告以便我可以看到下面的内容?
【问题讨论】:
标签: ios react-native show-hide
在您的 AppDelegate.m 文件中,您可以更改这一行:
jsCodeLocation = [NSURL URLWithString:@"http://localhost:8081/index.ios.bundle?platform=ios&dev=true"];
最后用dev=false替换dev=true。
【讨论】:
如果您想在特定构建中隐藏它们,因为您正在进行演示或其他操作,您可以编辑您的 Xcode 方案以使其成为发布构建,这些黄色警告将不会出现。此外,您的应用会运行得更快。
您可以通过执行以下操作为您的模拟器和真实设备编辑方案:
Product > Scheme > Edit Scheme...
Build Configuration 从 Debug 更改为 Release。【讨论】:
Release:没有警告,更快的应用程序!
Release 没有任何调试功能
有选择地隐藏某些警告(在升级到最新和最好的 RN 版本后无限期显示)的更好方法是在项目的公共 JS 文件中设置 console.ignoredYellowBox。例如,今天将我的项目升级到 RN 0.25.1 后,我看到了很多...
警告:ReactNative.createElement 已弃用...
我仍然希望能够看到来自 React-Native 的有用警告和错误消息,但我想消除这个特定的警告,因为它来自尚未包含 RN 0.25 中的重大更改的外部 npm 库。所以在我的 App.js 中我添加了这一行...
// RN >= 0.63
import { LogBox } from 'react-native';
LogBox.ignoreLogs(['Warning: ...']);
// RN >= 0.52
import {YellowBox} from 'react-native';
YellowBox.ignoreWarnings(['Warning: ReactNative.createElement']);
// RN < 0.52
console.ignoredYellowBox = ['Warning: ReactNative.createElement'];
这样我仍然会收到对我的开发环境有帮助的其他错误和警告,但我不再看到那个特定的错误和警告。
【讨论】:
根据 React Native Documentation,您可以通过将 disableYellowBox 设置为 true 来隐藏警告消息,如下所示:
console.disableYellowBox = true;
console.disableYellowBox 已删除,现在您可以使用:
import { LogBox } from 'react-native';
LogBox.ignoreLogs(['Warning: ...']); // Ignore log notification by message
LogBox.ignoreAllLogs();//Ignore all log notifications
忽略所有日志通知
【讨论】:
App.js(或Routes.js,具体取决于您的结构)也很有效。
对于那些试图从控制台禁用红色警告的人,截至 2 月 17 日,这提供绝对无用的信息,您可以在某处添加这行代码
console.error = (error) => error.apply;
禁用所有console.error
【讨论】:
(但不适用于您自己的代码)
为什么:在初始化一个新的 RN 应用程序时,Xcode 项目包含接近 100 个警告,这些警告会分散噪音(但可能无害)
解决方案:在相关目标的Build Settings下将inhibit all warnings设置为yes。
【讨论】:
要禁用黄框的地方
console.disableYellowBox = true;
在您的应用程序中的任何位置。通常在根文件中,因此它适用于 iOS 和 Android。
例如
export default class App extends React.Component {
render() {
console.disableYellowBox = true;
return (<View></View>);
}
}
【讨论】:
要禁用黄色框,请将console.disableYellowBox = true; 放置在应用程序的任何位置。通常在根文件中,因此它适用于 iOS 和 Android。
更多详情请查看official document
【讨论】:
在您的 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);
【讨论】:
console.disableYellowBox = true;
这适用于应用程序级别将它放在 index.js 文件中的任何位置
【讨论】:
在任何组件的生命周期方法下的 app.js 文件中。就像在 componentDidmount() 中 你必须添加这两个,排除任何都行不通。
console.ignoredYellowBox = ['Warning: Each', 'Warning: Failed'];
console.disableYellowBox = true;
【讨论】:
console.ignoredYellowBox = ['警告:每个','警告:失败'];
【讨论】:
console.disableYellowBox = true;
【讨论】:
add this line in your app main screen.
console.disableYellowBox = true;
例如:- 在 index.js 文件中
import { AppRegistry } from 'react-native';
import './src/utils';
import App from './App';
import { name as appName } from './app.json';
AppRegistry.registerComponent(appName, () => App);
console.disableYellowBox = true;
【讨论】:
我发现,即使我使用上述方法禁用了特定警告(黄框消息),警告在我的移动设备上被禁用,但它们仍被记录到我的控制台,这非常烦人和分散注意力。
为了防止警告被记录到您的控制台,您可以简单地覆盖 console 对象上的 warn 方法。
// This will prevent all warnings from being logged
console.warn = () => {};
甚至可以通过测试提供的消息来仅禁用特定警告:
// Hold a reference to the original function so that it can be called later
const originalWarn = console.warn;
console.warn = (message, ...optionalParams) => {
// Insure that we don't try to perform any string-only operations on
// a non-string type:
if (typeof message === 'string') {
// Check if the message contains the blacklisted substring
if (/Your blacklisted substring goes here/g.test(message))
{
// Don't log the value
return;
}
}
// Otherwise delegate to the original 'console.warn' function
originalWarn(message, ...optionalParams);
};
如果您不能(或不想)使用正则表达式来测试字符串,indexOf 方法也可以工作:
// An index of -1 will be returned if the blacklisted substring was NOT found
if (message.indexOf('Your blacklisted substring goes here') > -1) {
// Don't log the message
return;
}
请注意,此技术将过滤通过warn 函数的所有 消息,无论它们来自何处。
因此,请注意不要指定过于宽泛的黑名单,以抑制可能源自 React Native 以外的其他地方的其他有意义的错误。
另外,我相信 React Native 使用 console.error 方法来记录错误(红框消息),所以我假设这种技术也可以用来过滤掉特定的错误。
【讨论】:
RN >= 0.62
import {LogBox} from 'react-native'
在import下,添加
LogBox.ignoreLogs(['...']);
而不是'...', 您可以编写要隐藏的警告。 例如, 我有警告 VirtualizedLists 永远不应该...... 那么我可以写成
LogBox.ignoreLogs(['VirtualizedLists']);
如果你想添加另一个错误,你可以写成
LogBox.ignoreLogs(['VirtualizedLists','Warning:...']);
【讨论】:
对我来说,目前我使用的是 react native 0.64
import { LogBox } from 'react-native';
LogBox.ignoreLogs(['Warning: ...']); //Hide warnings
LogBox.ignoreAllLogs();//Hide all warning notifications on front-end
添加警告以准确指定要抑制的警告时,您需要准确地添加警告消息,如下所示(随机示例)
LogBox.ignoreLogs([
'Warning: Failed prop type: Invalid props.style key `tintColor` supplied to `Text`.',
]);
例如,在tintColor 或Text 周围使用单引号而不是反引号是行不通的。
【讨论】:
我喜欢将 console.disableYellowBox = true 放在文件根目录下,例如 App. 但这只是在我处于开发阶段时。
【讨论】: