开发者问题收集

React Navigation 错误 this.props.navigation.navigat

2019-04-12
1029

我有两个屏幕,我想通过单击使用 React 导航移动新屏幕。但是我收到错误

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);
2个回答

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

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);
Paras Korat
2019-04-12

您尚未像这样将登录屏幕添加到堆栈中

const MainNavigator = createStackNavigator({
  Login : {screen: Login},
  Home: {screen: SignUp}
});
Niranjan
2019-04-12