【问题标题】:Prevent Require Cycle warnings on terminal防止终端上的 Require Cycle 警告
【发布时间】:2019-03-28 07:04:20
【问题描述】:

我不断收到要求循环是允许的,但可能会导致未初始化的值。考虑重构以消除对循环的需要。 在我的终端上发出警告,它使调试我的程序变得如此困难。

我用过

YellowBox.ignoreWarnings([
  'Require cycle:', 
])

还有

console.disableYellowBox = true;

但都没有奏效。 YellowBox.ignoreWarnings 仅适用于移动设备上显示的警告,但不适用于我的 IDE 终端上的警告。如何防止在终端上收到这些警告。

【问题讨论】:

  • 没有办法隐藏它们,无论是IDEA终端还是系统一个AFAIK。 YellowBox.ignoreWarningsconsole.disableYellowBox 仅影响设备/模拟器

标签: react-native intellij-idea expo


【解决方案1】:

我注释掉了以下几行,它们不再出现在终端和设备中。

node_modules/metro/src/lib/polyfills/require.js(第 114 行)

  console.warn(
    "Require cycle: ".concat(cycle.join(" -> "), "\n\n") +
      "Require cycles are allowed, but can result in uninitialized values. " +
      "Consider refactoring to remove the need for a cycle."
  );

注释掉该警告后,重新启动项目解决了我的问题。 PS。我正在使用博览会。

【讨论】:

    【解决方案2】:

    隐藏黄色框不是解决方案,它是一种补丁。您应该查看您的代码,实际上存在问题是您的组件将相互要求。就像您制作了一个导入 B 的组件 A 并在 B 中导入 A。如果您显示完整的日志,我会进一步告诉您确切的组件。

    【讨论】:

      【解决方案3】:

      我在 node_modules/expo/build/environment/muteWarnings.fx.js 第 10 行注释掉了 console.warn。

      console.warn = function warn(...args) {
          // if (args.length > 0 &&
          //     typeof args[0] === 'string' &&
          //     (/^Require cycle: .*node_modules/.test(args[0]) ||
          //         /Use UIManager\.getViewManagerConfig\('LottieAnimationView'\) instead\./.test(args[0]) ||
          //         /ReactNative\.NativeModules\.LottieAnimationView\.getConstants/.test(args[0]))) {
          //     return;
          // }
          // originalWarn.apply(console, args);
      };
      

      【讨论】:

        【解决方案4】:

        我选择了一种更安全的方法 :) 我使用了补丁包来确保我仍然看到自己的错误...嗯...需要循环 :)

        地铁+0.59.0.patch

        diff --git a/node_modules/metro/.DS_Store b/node_modules/metro/.DS_Store
        new file mode 100644
        index 0000000..21ab2f3
        Binary files /dev/null and b/node_modules/metro/.DS_Store differ
        diff --git a/node_modules/metro/src/lib/polyfills/require.js b/node_modules/metro/src/lib/polyfills/require.js
        index 8c04756..3b208f6 100644
        --- a/node_modules/metro/src/lib/polyfills/require.js
        +++ b/node_modules/metro/src/lib/polyfills/require.js
        @@ -114,11 +114,14 @@ function metroRequire(moduleId) {
                 .map(id => (modules[id] ? modules[id].verboseName : "[unknown]")); // We want to show A -> B -> A:
         
               cycle.push(cycle[0]);
        -      console.warn(
        -        `Require cycle: ${cycle.join(" -> ")}\n\n` +
        +      const cycleString = cycle?.join(" -> ");
        +      if (!(cycleString?.startsWith('node_modules/'))) {
        +        console.warn(
        +          `Require cycle: ${cycleString}\n\n` +
                   "Require cycles are allowed, but can result in uninitialized values. " +
                   "Consider refactoring to remove the need for a cycle."
        -      );
        +        );
        +      }
             }
           }
         
        

        【讨论】:

          【解决方案5】:

          我对此问题的解决方案,希望对其他人有所帮助:

          import { AppRegistry, LogBox } from 'react-native';
          import App from './App';
          import { name as appName } from './app.json';
          
          LogBox.ignoreLogs(['Require cycle:']);
          
          AppRegistry.registerComponent(appName, () => App);
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2013-08-19
            • 2011-05-18
            • 1970-01-01
            • 1970-01-01
            • 2012-01-22
            • 2016-09-13
            • 2014-04-21
            • 2016-02-07
            相关资源
            最近更新 更多