React-Native Navigator“未定义不是对象(评估this.props.navigator)”错误

时间:2017-01-18 04:16:54

标签: javascript ios react-native native navigator

我非常非常了解React Native(和Stackoverflow),我一直在使用Navigator切换屏幕,在研究了两天的帮助后我仍然无法做到这一点,所以我终于求助于发布一个问题。我的代码给了我一个:“Undefined不是一个对象(评估this.props.navigator)”错误,这是我的代码:

导航器类:

import HomeScreen from './HomeScreen';

import LoginScreen from './LoginScreen'

export default class Navigation extends Component {
    render() {

    return (

       <Navigator

       initialRoute={{id:"Login"}}

        renderScene={(route, navigator) =>
        {return this.renderScene(route, navigator)}}
      />
    );
}

renderScene(route, navigator) {

    switch(route.id) {

        case "Login":

            return (<LoginScreen {...route.passProps} navigator={navigator}/>);

        case "Home": 

            return (<HomeScreen {...route.passProps} navigator={navigator}/>);

    }
}
} 

和LoginScreen.js文件中的LoginButton组件

class LoginButton extends Component {

    render() {
       const routes = [{id:'LoginScreen'},{id:'HomeScreen'}];
        return (

           <View style={{height:50}}>
            <Container>
                <Content>
                    <Button style={{backgroundColor: '#eeb211'}} onPress={this.nextScreen.bind(this)} rounded >              Login Next Screen           </Button>
                </Content>
            </Container>
            </View>
        );
    }
   nextScreen() {
     this.props.navigator.push({id:"Home"})

   }
}

1 个答案:

答案 0 :(得分:0)

尝试以下:

<Navigator
        style={{ flex:1 }}
        ref={(nav) => { navigator = nav; }}
        initialRoute={{id:"Login"}}
        renderScene={(route, navigator) =>
         {return this.renderScene(route, navigator)}}
      />