【问题标题】:Invariant Violation: requireNativeComponent: "RNCWebView" was not found in the UIManager不变违规:requireNativeComponent:在 UIManager 中找不到“RNCWebView”
【发布时间】:2020-09-02 01:06:31
【问题描述】:

您好朋友,我在将 react-native-webview 链接到我的项目后收到此错误

Invariant Violation: requireNativeComponent: "RNCWebView" was not found in the UIManager.

    This error is located at:
        in RNCWebView (at WebView.android.js:250)
        in RCTView (at WebView.android.js:253)
        in WebView (at App.js:7)
        in App (at renderApplication.js:45)
        in RCTView (at AppContainer.js:109)
        in RCTView (at AppContainer.js:135)
        in AppContainer (at renderApplication.js:39)

还有我的代码

import React, { Component } from 'react';
import { WebView } from 'react-native-webview';

export default class App extends Component {
  render() {
    return (
      <WebView
        originWhitelist={['*']}
        source={{ html: '<h1>Hello world</h1>' }}
      />
    );
  }
}

Error Image in my android

【问题讨论】:

  • 你能告诉我们更多关于你想使用它的项目和它的代码吗?您是否检查过错误中提到的文件(即 WebView.android.js 中)的行在说什么?你可以给我们这个代码,也许这是一个开始。谢谢。

标签: react-native react-native-webview


【解决方案1】:

运行react-native link react-native-webview

检查:https://github.com/react-native-community/react-native-webview/issues/140

【讨论】:

  • 如果您使用的是 React Native 0.60+,则不再需要此操作,只需 cd iospod install,可能重新启动 xCode,可能在 Xcode 中单击菜单上的 Product > Clean Build Folder,然后你应该很高兴
【解决方案2】:

我有同样的问题。尝试将此行添加到您的 podfile 中:-

pod 'react-native-webview', :path =&gt; PROJECT_PATH + 'react-native-webview'

然后转到 ios 文件夹 cd ios 并安装 pod pod install

它已经为我解决了这个问题。

【讨论】:

  • 我确实运行了链接,我认为它自动将该行添加到我的 podfile 中,只需要 cd ios adn pod install 就可以了?
  • 我不会在 React Native 0.60+ 上这样做。只有cd iospod install 就够了
【解决方案3】:

试试这些步骤。

  1. 删除 pods 文件夹和 podfile.lock
  2. npm 卸载 react-native-webview
  3. npm i react-native-webview
  4. cd ios
  5. pod 安装
  6. (npx) react-native run-ios
  7. 重置缓存

【讨论】:

  • 我忘了安装 pod 兄弟
  • 别忘了重启模拟器并重建你的应用
【解决方案4】:

我已经完成了以下步骤:

 1. cd ios
 2. rm Podfile.lock
 3. pod install

这安装了缺少的库,即 react-native-webview 并将下面的行添加到 Podfile 文件中。

 pod 'react-native-webview', :path => '../node_modules/react-native-webview'

我工作的环境:

"react": "16.9.0",
"react-native": "0.61.5"

【讨论】:

  • 如果您使用的是 React Native 0.60+,请不要修改 Podfile。现在有自动链接。
【解决方案5】:

我对这个问题的解决方案如下:

安卓

1 在你的 android/settings.gradle 中添加这个

include ':react-native-webview'
project(':react-native-webview').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-webview/android')

2 在你的 android/app/build.gradle 中添加这个

dependencies {
    implementation project(':react-native-webview')
    ....

3 在您的 MainApplication.java 中:

import com.reactnativecommunity.webview.RNCWebViewPackage;//add this import
....

public class MainApplication extends Application implements ReactApplication {

  private final ReactNativeHost mReactNativeHost = new ReactNativeHost(this) {
    @Override
    public boolean getUseDeveloperSupport() {
      return BuildConfig.DEBUG;
    }

    @Override
    protected List<ReactPackage> getPackages() {
      return Arrays.<ReactPackage>asList(
          new MainReactPackage(),
          new RNCWebViewPackage(),//add line
          ....

IOS

1 在您的 ios/Podfile 中添加以下行,然后打开您的终端,转到您的项目 ios 文件夹并运行 pod install

...
  pod 'react-native-webview', :path => '../node_modules/react-native-webview'
...

【讨论】:

    【解决方案6】:

    使用 React Native 0.60+,无需向 podfile 添加任何内容。 再试一次cd ios &amp;&amp; pod install

    如果没有帮助,请尝试重新启动模拟器并重新构建应用程序。 重启 Xcode 也是个好主意。

    您可以选择清理 XCode 中的构建文件夹

    【讨论】:

    • 如果 react-native version > 0.0.60 with autolinking 的正确答案
    【解决方案7】:

    我之前也遇到过同样的问题,我试过这个:

    pod 'react-native-webview', :path =&gt; PROJECT_PATH + 'react-native-webview'

    【讨论】:

      【解决方案8】:

      在安装和链接 webview 包 (npx react-native run-android) 后尝试重新构建您的项目

      【讨论】:

      • 我已经解决了这个问题,感谢兄弟的支持。
      【解决方案9】:

      确保你做到了:

      1. npx pod-install
      2. npx react-native run-ios

      安装包后你。需要更新 pod,然后再次运行项目。

      【讨论】:

        【解决方案10】:

        按照这些对我有用的简单步骤:

        1. 重建应用程序。运行:
        npx react-native run-android
        
        1. 运行:
        react-native link react-native-webview
        
        1. 检查RNCWebViewPackage 是否导入到MainApplication.java 文件中,如下所示:
        import com.reactnativecommunity.webview.RNCWebViewPackage;
        

        注意:我使用的是react-native@0.64.2

        【讨论】:

          【解决方案11】:

          无需添加任何内容或使用 React Native 0.60+ 的链接。 在我的情况下,它是 android,我只需使用以下命令再次构建项目: $ npm 运行安卓

          【讨论】:

            猜你喜欢
            • 2020-05-06
            • 2019-08-27
            • 2021-06-29
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2022-10-23
            相关资源
            最近更新 更多