前言:ViewPager重复引用的两个错误
错误1:Tried to register two views with the same name "RNCViewPager"
错误2:Invariant Violation: requireNativeComponent: "RNCViewPager" was not found in the UIManager.
接下来是解决问题的流程
run-android报错如下:
Tried to register two views with the same name RNCViewPager
直译错误是重复引用了ViewPager组件
我所使用的版本
react-native 0.61.5
排错记录:
通过全局搜索最终发现有两个组件引用了viewpager
1. "@ant-design/react-native": "^3.2.2", 这里这个组件使用了 viewpager 1.1.7
2. "react-native-scrollable-tab-view": "^1.0.0", 这里这个组件使用了 viewpager 2.0.1
通过查询资料以及通过github issues发下以下同样的问题
https://github.com/ant-design/ant-design-mobile-rn/issues/697
https://github.com/ptomasroos/react-native-scrollable-tab-view/issues/1056
根据参考大牛做法最终修改自己的项目根目录下的 package.json 文件,增加resolutions来统一viewpager的版本为2.0.1。
修改如下:
"dependencies": {
.
.
.
},
"resolutions": {
"@react-native-community/viewpager": "^2.0.1"
}
在项目根目录运行 yarn 后,再次react-native run-android成功跑起了项目。但进入包含此组件功能的页面时又再次报错!!!如下:
Invariant Violation: requireNativeComponent: "RNCViewPager" was not found in the UIManager.
看到这个错误感觉还是版本问题,接下来我又对版本进行修改,以及在dependencies中也增加了viewpager的引用,升级版本为"^4.1.0"。
修改如下:
"dependencies": {
"@react-native-community/viewpager": "^4.1.0"
},
"resolutions": {
"@react-native-community/viewpager": "^4.1.0"
}
再次在项目根目录运行 yarn 后,成功解决了这个问题。
注:npm install 并不支持 resolutions,需要使用 yarn 才能解决这个问题。