gpt4 book ai didi

react-native - react 导航错误 this.props.navigation.navigat

转载 作者:行者123 更新时间:2023-12-04 05:14:37 25 4
gpt4 key购买 nike

我有两个屏幕,我想单击以使用 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);

最佳答案

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

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

只需制作 登录的单独组件 screen 将其添加到堆栈顶部,然后您就可以导航了。

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

登录.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);

关于react-native - react 导航错误 this.props.navigation.navigat,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55649502/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com