反应导航错误this.props.navigation.navigate

问题描述 投票:3回答:2

我有两个屏幕,我想点击以使用反应导航移动新屏幕。但是我收到了一个错误

this.props.navigation.navigate

这是我的代码。谁知道什么是问题。我遵循每一步。

    import React, {Component} from 'react';
    import { AppRegistry, View, Text,StyleSheet, TextInput, Image, ScrollView, Button, TouchableOpacity} from 'react-native';
    import { createStackNavigator, createAppContainer } from 'react-navigation';

    import SignUp from './SignUp';

    class Login extends  React.Component{

    constructor(props){
        super(props);
        this.state = { text: 'Useless Placeholder' };
    }
        render(){
    const {navigate} = this.props.navigation;
       return(
          <View>
              <ScrollView showsVerticalScrollIndicator={false}>
                <View style={styles.button}>
                         <TouchableOpacity>
                            <Text style={styles.buttonText}>SIGN IN</Text>
                          </TouchableOpacity>
                       </View>
                        <View style={styles.createAccount}>
                             <Text style={styles.createText}
                             onPress={() => this.props.navigation.navigate("Home")}
                             >Create new account</Text>
                        </View>
               </View>
            </ScrollView>
          </View>

       );
    }

}


module.exports = Login;


const MainNavigator = createStackNavigator({
  Home: {screen: SignUp}
});

export default createAppContainer(MainNavigator);
react-native react-navigation
2个回答
1
投票

您的登录屏幕不是您的堆栈的一部分。

const MainNavigator = createStackNavigator({
  Home: {screen: SignUp}
});

只需创建一个单独的登录屏幕组件,将其添加到堆栈顶部,然后就可以导航了。

像这样

const MainNavigator = createStackNavigator({
  Login:{screen:Login}
  Home: {screen: SignUp}
});

Login.js

import React, {Component} from 'react';
import { AppRegistry, View, Text,StyleSheet, TextInput, Image, ScrollView, Button, TouchableOpacity} from 'react-native';

    export default class Login extends  React.Component{
    constructor(props){
        super(props);
        this.state = { text: 'Useless Placeholder' };
    }
        render(){
    const {navigate} = this.props.navigation;
       return(
          <View>
              <ScrollView showsVerticalScrollIndicator={false}>
                <View style={styles.button}>
                         <TouchableOpacity>
                            <Text style={styles.buttonText}>SIGN IN</Text>
                          </TouchableOpacity>
                       </View>
                        <View style={styles.createAccount}>
                             <Text style={styles.createText}
                             onPress={() => this.props.navigation.navigate("Home")}
                             >Create new account</Text>
                        </View>
               </View>
            </ScrollView>
          </View>

       );
    }

MainNavigator.js

import React, {Component} from 'react';
    import { AppRegistry, View, Text,StyleSheet, TextInput, Image, ScrollView, Button, TouchableOpacity} from 'react-native';
    import { createStackNavigator, createAppContainer } from 'react-navigation';

import SignUp from './SignUp';
import Login from './Login'


const MainNavigator = createStackNavigator({
  Login: {screen:Login},
  Home: {screen: SignUp}
});

export default createAppContainer(MainNavigator);

1
投票

你没有像这样在堆栈中添加登录屏幕

const MainNavigator = createStackNavigator({
  Login : {screen: Login},
  Home: {screen: SignUp}
});
© www.soinside.com 2019 - 2024. All rights reserved.