ReactNative错误总结

以下错误的环境是:
React:15.4.2
ReactNative:0.40.0

1 Element type is invalid

错误信息.png

ES5的导出导入语法:

module.exports = Page2;
var NaviBar = require('./NaviBar');

ES6的导入导出语法:

export  default class Page2 extends Component
import NaviBar from './NaviBar';

Demo中使用了错误的导入方法

import { AppRegistry } from 'react-native';
import { FirstView } from './Demo/ViewDemo.js';

正确的是

import { AppRegistry } from 'react-native';
import FirstView from './Demo/ViewDemo.js';   //自定义的不能使用{}

Cannot read property 'navigator' of undefined

错误信息.png

原因是:When you create components as ES6 classes, methods are not bound to instance automatically。就是在初始化的时候,方法没有绑定实例对象。
解决方案:goTo方法绑定实例对象

 constructor(props, context) {
        super(props, context);
        this.goTo = this.goTo.bind(this);
 }
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容