React native报错集锦

1.错误:View config not found for name customView

组件的名称首字母必须是大写的,小写的不行,改成CustomView

2.错误:Invariant Violation

注意import引入时 的{}
如果是 export default class XXX import的时候不需要有中括号{}
如果是export class CustomView import的时候需要中括号{}
例:import {CustomView}

3.错误:[error: Error: watch EMFILE]

终端输入:
(1) sudo launchctl limit maxfiles 2048 unlimited
(2) npm uninstall -g react-native-cli
(3) npm install -g react-native-cli
(4) brew update
(5) brew install watchman
解决方案参考地址

4.错误:error: bundling failed: Error: Unable to resolve module ./../react-transform-hmr/lib/index.js from /Users/sturm/Desktop/testpro/App.js: The module ./../react-transform-hmr/lib/index.js could not be found from /Users/sturm/Desktop/testpro/App.js. Indeed, none of these files exist:

这个问题是react native最新版本的坑,官方挖的。(如遇到其他坑:可以到gayhub查找,这个最靠谱!)
解决方案是:首先关掉正在运行项目的终端命令窗口,重新打开窗口
终端输入:

#mac下的操作:
# Clean cache
  rm -rf $TMPDIR/react-*; rm -rf $TMPDIR/haste-*; rm -rf $TMPDIR/metro-*; watchman watch-del-all
# Start Metro Bundler directly
  react-native start
#打开另一个命令窗口,cd到项目所在目录,运行react-native run-ios
  react-native run-ios

#window下的操作:
# Clean cache
react-native start --reset-cache
#打开另一个命令窗口,cd到项目所在目录,运行react-native run-android
  react-native run-android

5.错误:Navigator is deprecated and has been removed from this package.

Navigator从react-native库中移除,添加到了react-native-deprecated-custom-components库中;
解决方案:

(1). 安装react-native-deprecated-custom-components库

npm install  react-native-deprecated-custom-components --save

(2). 导入react-native-deprecated-custom-components库

import {Navigator} from 'react-native-deprecated-custom-components';

6.错误:TypeError: Cannot read property 'string' of undefined

使用 PropTypes 进行类型检查
注意: 从 React v15.5 开始 ,React.PropTypes 助手函数已被弃用,我们建议使用 prop-types 库 来定义.

解决方案:

(1). 安装prop-types库

npm install prop-types --save

(2). 导入prop-types库

import PropTypes from 'prop-types';

7.错误:RN打包(iOS)错误:/node_modules/react-native/Libraries/Image/RCTImageCache.m:28:55: Enum values with underlying type ‘NSInteger’ should not be used as format arguments; add an explicit cast to ‘long’ instead

解决方案:

(1). Xcode降级到8.3;
(2). 将RN版本升级到最高;
(3). 手动fixed问题: %zd 替换为 %ld ; eventLag 替换为 (long)eventLag;

return [NSString stringWithFormat:@"%@|%g|%g|%g|%ld|%@", 
imageTag, size.width, size.height, scale, (long)resizeMode, responseDate];
RCTLogWarn(@"Native TextInput(%@) is %ld events ahead of JS - try to make your JS faster.", self.text, (long)eventLag);

由于用的是旧版本的RN,怕升级了版本又会出现别的错误,所以最简单还是用第三个方案。
修改完后,我这边又报错了:
在RCTResizeMode.h文件里,#import
RCTConvert file not found
日了,最后找到的解决方案是:
选择RCTImage.xcodeproj,选择Project"RCTImage",然后 搜索 "Search Paths", "Header Search Paths", 修改这一行,若没有就新增一行:

$(SRCROOT)/../../../react-native/React

to:

$(SRCROOT)/../react-native/React       /*若没有,就新增这一行*/

注意:此处 $(SRCROOT) 会映射到ios目录后面加 ../ 表示父级目录。

你可能感兴趣的:(React native报错集锦)