【问题标题】:Invariant Violation: ViewPropTypes has been removed from React Native. Migrate to ViewPropTypes exported from 'deprecated-react-native-prop-types'Invariant Violation:ViewPropTypes 已从 React Native 中移除。迁移到从 \'deprecated-react-native-prop-types\' 导出的 ViewPropTypes
【发布时间】:2022-08-24 18:43:44
【问题描述】:

由于 ViewPropTypes 已从 \'react-native\' 中删除,并且使用它的包没有更新。构建应用程序后出现此错误

ERROR Invariant Violation: ViewPropTypes 已从 React Native 中删除。迁移到从 \'deprecated-react-native-prop-types\' 导出的 ViewPropTypes。
错误不变违规:模块 AppRegistry 不是已注册的可调用模块(调用 runApplication)。错误的常见原因是应用程序入口文件路径不正确。 当 JS 包损坏或加载 React Native 时出现早期初始化错误时,也会发生这种情况。 错误不变违规:模块 AppRegistry 不是已注册的可调用模块(调用 runApplication)。错误的常见原因是应用程序入口文件路径不正确。 当 JS 包损坏或加载 React Native 时出现早期初始化错误时,也会发生这种情况。

我正在使用的软件包:

\"@react-native-clipboard/clipboard\": \"^1.10.0\",
\"@react-native-community/checkbox\": \"^0.5.12\",
\"@react-native-firebase/app\": \"^14.11.0\",
\"@react-native-firebase/auth\": \"^14.9.4\",
\"@react-native-firebase/database\": \"^14.11.0\",
\"@react-native-firebase/firestore\": \"^14.11.0\",
\"@react-native-google-signin/google-signin\": \"^7.2.2\",
\"@react-native-masked-view/masked-view\": \"github:react-native-masked-view/masked-view\",
\"@react-native-picker/picker\": \"^2.4.1\",
\"@react-navigation/bottom-tabs\": \"^6.3.1\",
\"@react-navigation/native\": \"^6.0.10\",
\"@react-navigation/stack\": \"^6.2.1\",
\"axios\": \"^0.27.2\",
\"base-64\": \"^1.0.0\",
\"num-words\": \"^1.2.2\",
\"numeral\": \"^2.0.6\",
\"pdf-lib\": \"^1.17.1\",
\"react\": \"17.0.2\",
\"react-native\": \"^0.69.0\",
\"react-native-blob-util\": \"^0.16.1\",
\"react-native-country-picker-modal\": \"^2.0.0\",
\"react-native-date-picker\": \"^4.2.2\",
\"react-native-fbsdk-next\": \"^8.0.5\",
\"react-native-fs\": \"^2.20.0\",
\"react-native-gesture-handler\": \"^2.5.0\",
\"react-native-html-to-pdf\": \"^0.12.0\",
\"react-native-pdf\": \"^6.5.0\",
\"react-native-picker-select\": \"^8.0.4\",
\"react-native-progress\": \"^5.0.0\",
\"react-native-radio-input\": \"^0.9.4\",
\"react-native-ratings\": \"^8.1.0\",
\"react-native-safe-area-context\": \"^4.2.5\",
\"react-native-screens\": \"^3.13.1\",
\"react-native-share\": \"^7.5.0\",
\"react-native-signature-canvas\": \"^4.3.1\",
\"react-native-vector-icons\": \"^9.1.0\",
\"react-native-webview\": \"^11.21.2\",
\"react-scripts\": \"^5.0.1\"

有什么解决办法吗?

  • 您是否尝试过错误消息所说的内容? Migrate to ViewPropTypes exported from \'deprecated-react-native-prop-types\'.
  • 因为当我这样做时,我会得到这个TypeError: undefined is not an object (evaluating \'ReactCurrentActQueue$1.isBatchingLegacy\')

标签: react-native


【解决方案1】:

我可以推荐执行此github 讨论中概述的步骤。

脚步

  1. 安装patch-package,稍后将用于使更改更加持久。

  2. 安装弃用-react-native-prop-types通过运行 npm install deprecated-react-native-prop-typesyarn add deprecated-react-native-prop-types

  3. 现在你必须破解节点模块.去node_modules/react-native/index.js从第 436 行开始并更改:

    // Deprecated Prop Types
    get ColorPropType(): $FlowFixMe {
      invariant(
        false,
        "ColorPropType has been removed from React Native. Migrate to " +
          "ColorPropType exported from 'deprecated-react-native-prop-types'.",
     );
    },
    get EdgeInsetsPropType(): $FlowFixMe {
      invariant(
        false,
        "EdgeInsetsPropType has been removed from React Native. Migrate to " +
          "EdgeInsetsPropType exported from 'deprecated-react-native-prop-types'.",
      );
    },
    get PointPropType(): $FlowFixMe {
      invariant(
        false,
        "PointPropType has been removed from React Native. Migrate to " +
         "PointPropType exported from 'deprecated-react-native-prop-types'.",
     );
    },
    get ViewPropTypes(): $FlowFixMe {
     invariant(
       false,
       "ViewPropTypes has been removed from React Native. Migrate to " +
         "ViewPropTypes exported from 'deprecated-react-native-prop-types'.",
     );
    },
    

    对此:

      // Deprecated Prop Types
      get ColorPropType(): $FlowFixMe {
        return require("deprecated-react-native-prop-types").ColorPropType
      },
      get EdgeInsetsPropType(): $FlowFixMe {
        return require("deprecated-react-native-prop-types").EdgeInsetsPropType
      },
      get PointPropType(): $FlowFixMe {
        return require("deprecated-react-native-prop-types").PointPropType
      },
      get ViewPropTypes(): $FlowFixMe {
        return require("deprecated-react-native-prop-types").ViewPropTypes
      },
    
    1. 运行npx patch-package react-native 保存补丁。

    2. 重建应用程序。

    唯一要记住的是,每次升级到 react-native 时都需要重新应用此补丁,或者直到有问题的库更新为从 deprecated-react-native-prop-types 导入。

    goguda 提供此答案的道具。

【讨论】:

  • 嘿,这是一个很好的解决方案,不仅可以解决此错误,还可以解决此类第三方库的其他情况。多谢!
  • 多亏了goguda。这个解决方案真的救了我的命,因为我需要修复几个第三方库。很高兴分享它并帮助其他人。
【解决方案2】:

我有同样的错误,我按照以下步骤操作,我已经解决了。它的出现是因为 react-native 已经从库中删除了 view prop 类型,但仍然有一些外部模块需要使用它。

  1. 在终端运行命令

    npm 我不推荐使用-react-native-prop-types@2.2.0

    1. 转到节点模块并找到您正在使用的模块,在我的情况下它是“react-native-camera”

    2. 导航到模块文件的“src”文件夹(在我的情况下它在 RNCamera.js 中)并查找

     import {
          findNodeHandle,
          Platform,
          NativeModules,
          ViewPropTypes,
          requireNativeComponent,
          View,
          ActivityIndicator,
          Text,
          StyleSheet,
          PermissionsAndroid,
        } from 'react-native';
    

    在下面的代码中删除 ViewPropTypes 并在下面粘贴以下命令

    import { ViewPropTypes } from 'deprecated-react-native-prop-types';
    

    保存文件并再次运行,希望对您有帮助 <3

【讨论】:

    【解决方案3】:

    我在使用 react-native-camera 时遇到了同样的错误。我修复它安装 npm i deprecated-react-native-prop-types@2.2.0 并在 node_modules/react-native-camera 中替换文件中的所有导入。 从'react-native'导入{ ViewPropTypes }; 为了 导入 { ViewPropTypes } from 'deprecated-react-native-prop-types';

    【讨论】:

      【解决方案4】:

      升级到新的 RN v0.69 后,我遇到了同样的问题。更改 node_modules/react-native-camera 将在本地解决问题。但是,我们知道,当需要从头开始安装节点依赖项时——例如在 CI/CD 过程中——需要重新完成第 3 方库的更改。因此,我建议的一个更好的解决方案是使用babel-plugin-module-resolver 在构建运行时级别拦截react-native-camera 组件,如此处建议的https://github.com/psycheangel/deprecated-with-module-resolver。我做了另一种方法,因为这个解决方案对我不起作用,因为它更通用并且与我项目中的其他东西发生冲突。仍然使用相同的 babel 解析器插件,但要解决 react-native-camera 组件的特定问题,这是我基于 psycheangel 的上述解决方案所做的方式:

      步骤1

      安装 babel 解析器插件和转身依赖:

      npm install --save-dev babel-plugin-module-resolver deprecated-react-native-prop-types
      

      或者

      yarn add --dev babel-plugin-module-resolver deprecated-react-native-prop-types
      

      第2步

      使用以下代码创建resolver/react-native/index.js

      import * as ReactNative from 'react-native'
      import * as DeprecatedPropTypes from 'deprecated-react-native-prop-types'
      
      delete ReactNative['ColorPropType']
      delete ReactNative['EdgeInsetsPropType']
      delete ReactNative['ImagePropTypes']
      delete ReactNative['PointPropType']
      delete ReactNative['TextInputPropTypes']
      delete ReactNative['TextPropTypes']
      delete ReactNative['ViewPropTypes']
      
      module.exports = {
        ...ReactNative,
        ...DeprecatedPropTypes,
      }
      

      第 3 步

      将模块解析器插件配置添加到babel.config.js

      const path = require('path')
      module.exports = {
        presets: ['module:metro-react-native-babel-preset'],
        plugins: [
          [
            'module-resolver',
            {
              root: ['.'],
              resolvePath(sourcePath, currentFile) {
                if (
                  sourcePath === 'react-native' &&
                  currentFile.includes('react-native-camera/src/RNCamera.js')
                ) {
                  console.log('resolver', sourcePath, currentFile)
                  return path.resolve(__dirname, 'resolver/react-native')
                }
              },
            },
          ],
        ],
      }
      

      【讨论】:

      • 不为我工作。
      猜你喜欢
      • 2022-06-10
      • 2022-06-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-01-25
      • 2022-09-25
      • 2018-11-24
      • 1970-01-01
      相关资源
      最近更新 更多