React Native Issue:undefined不是对象(evaluationg'his.props.navigator.push')

时间:2017-10-18 19:15:26

标签: javascript react-native navigator

我是一个反应原生的新手,我试图在屏幕之间导航......我按照一个简单的教程,不幸的是,当我按下“To Stats页面”TouchableHighlight我得到错误:undefined不是一个对象(评估'这个' .props.navigator.push')。附上主屏幕的屏幕截图和错误(单击TO STATS PAGE),下面是代码(我包含了我认为错误发生的评论):

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);

Home Screen

Error when touching to stats page, using expo

1 个答案:

答案 0 :(得分:-1)

伙计们,我认为这是最好的!修复是Navi应该导出不是主...