【问题标题】:How to suppress warning due to a third-party PropTypes library in react native如何在 react native 中抑制由于第三方 PropTypes 库引起的警告
【发布时间】:2016-08-11 23:51:29
【问题描述】:

我在 React Native 中使用样式表时收到很多警告,如下图所示。

Warning on iOS simulator

如何抑制?

【问题讨论】:

    标签: reactjs react-native


    【解决方案1】:

    无法禁用特定组件的警告,但您可以在应用中禁用不同类型的警告。要禁用所有警告,请使用:

    console.disableYellowBox = true;
    

    要仅禁用以给定字符串开头的某些警告,请指定要过滤掉的前缀数组:

    console.ignoredYellowBox = ['Warning: ...'];
    

    例如,对于问题中的警告,您可以写:

    console.ignoredYellowBox = [
      'Warning: You are manually calling a React.PropTypes validation',
    ];
    

    【讨论】:

    • @ide 我需要像这样将它直接放入我的组件中吗 render() { console.disableYelowBox = true; return ( ); },
    • 把它放在应用启动时运行的地方。 Grep for disableYellowBox 并查看实现以获得更好的想法。
    • @WaleedArshad 我把console.ignoredYellowBox 放在主组件类的定义之上。
    • 哇,这让我的 propType 检查完全没用了。不幸的是,React 在 propType 检查失败时不会抛出异常,所以我必须查看警告。不幸的是,警告被一些第三方污染了。这使我的类型检查完全无用。
    • 您应该更新您对新 YellowBox API 的回答。 console.ignoredYellowBox 不再起作用了。
    【解决方案2】:

    YellowBox API 已从 ReactNative 0.52 更改:

    完全禁用 YellowBox:console.disableYellowBox = true;

    忽略一些警告:

    import { YellowBox } from 'react-native';
    YellowBox.ignoreWarnings(['Warning text here...']);
    

    https://facebook.github.io/react-native/docs/debugging#warnings

    【讨论】:

      【解决方案3】:

      在 React v0.63 中再次更新。

      import { LogBox } from 'react-native';
      
      LogBox.ignoreLogs(['Warning text here...']);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-11-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多