报错Invariant Violation:重复注册RNCWebView视图,求解决办法
解决"Invariant Violation: Tried to register two views with the same name RNCWebView"错误
检查重复依赖
- 项目可能同时安装了
react-native-webview和其他内置旧版webview的依赖(比如部分UI库)。执行npm list react-native-webview或yarn list react-native-webview查看依赖树,找出重复引入的包。 - 若发现重复,在
package.json中添加resolutions字段(npm需先安装npm-force-resolutions,yarn直接支持),强制统一webview版本:
之后重新安装依赖:"resolutions": { "react-native-webview": "13.6.3" // 替换成你项目兼容的版本 }npm install或yarn install
- 项目可能同时安装了
清理原生端缓存
- iOS:
- 删除
ios/Pods文件夹和Podfile.lock - 执行
pod install --repo-update - 通过Xcode清理编译缓存:菜单栏 -> Product -> Clean Build Folder
- 删除
- Android:
- 删除
android/.gradle、android/app/build文件夹 - 执行
./gradlew clean(Windows系统用gradlew clean)
- 删除
- iOS:
排查自动链接冲突
- 手动检查
ios/Podfile,如果重复引入了RNCWebView的pod,删除多余条目后重新执行pod install - 检查
android/settings.gradle和android/app/build.gradle,若有重复添加的webview依赖配置,删除后重新编译
- 手动检查
重装webview依赖
- 先卸载:
npm uninstall react-native-webview或yarn remove react-native-webview - 清理缓存:
npm cache clean --force或yarn cache clean - 重新安装对应兼容版本:
npm install react-native-webview@xxx或yarn add react-native-webview@xxx(xxx替换为具体版本号) - RN 0.60+自动完成链接,旧版本需额外执行
react-native link react-native-webview
- 先卸载:
清除metro缓存启动
- 启动项目时添加参数清除metro缓存:
react-native start --reset-cache
- 启动项目时添加参数清除metro缓存:
内容的提问来源于stack exchange,提问作者Himanshu Manchanda
相关产品推荐
相关产品推荐

