以下錯誤的環(huán)境是:
React:15.4.2
ReactNative:0.40.0
1 Element type is invalid
錯誤信息.png
ES5的導(dǎo)出導(dǎo)入語法:
module.exports = Page2;
var NaviBar = require('./NaviBar');
ES6的導(dǎo)入導(dǎo)出語法:
export default class Page2 extends Component
import NaviBar from './NaviBar';
Demo中使用了錯誤的導(dǎo)入方法
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);
}