我是一个反应原生新手,正在尝试在屏幕之间导航......我遵循了一个简单的教程,不幸的是,当我按下"到统计页面"可触摸突出显示时,我收到错误:undefined不是一个对象(评估"this.props.navigator.push")。附上主屏幕和错误的屏幕截图(单击"到统计"页面时),下面是代码(我包含了我认为错误发生位置的评论):
import React, { Component } from 'react';
import {AppRegistry,StyleSheet,TouchableHighlight,Navigator,Text,View} from 'react-native';
class Navi extends Component {
render() {
return (
<Navigator
initialRoute={{ name: 'main' }}
renderScene={this.renderScene.bind(this)}
/>
);
}
renderScene(route, navigator) {
if(route.name == 'main') {
return <Main navigator={navigator} />
}
if(route.name == 'stats') {
return <Stats navigator={navigator} />
}
}
}
export default class Main extends Component {
// THINK THE ERROR IS HERE
navigate(routeName) {
this.props.navigator.push({
name: routeName
});
}
render() {
return (
<View style={styles.container}>
<Text style={styles.welcome}>
Main
</Text>
<TouchableHighlight onPress={this.navigate.bind(this, "stats")}>
<Text>TO STATS PAGE</Text>
</TouchableHighlight>
</View>
);
}
}
class Stats extends Component {
render() {
return (
<View style={styles.container}>
<Text style={styles.welcome}>
Stats
</Text>
<TouchableHighlight onPress={()=>{this.props.navigator.pop()}}>
<Text>BACK</Text>
</TouchableHighlight>
</View>
);
}
}
const styles = StyleSheet.create({
container: {marginTop:20,flex: 1,justifyContent: 'center',alignItems: 'center',
backgroundColor: '#F5FCFF',},
welcome: {fontSize: 20,textAlign: 'center',margin: 10,},
});
AppRegistry.registerComponent('navi', () => Navi);
主屏幕
使用expo触摸统计信息页面时出错
伙计们,我想说是最好的!!修复是 Navi 应该导出而不是主要...