[英]How can I navigate from one screen to another via a button press using stack navigator?
I am looking to navigate from my login screen to my registration screen when a touchable opacity style button is pressed. 我希望在按下可触摸的不透明样式按钮时从登录屏幕导航到注册屏幕。 I also want to set the initial screen to the login screen.
我还想将初始屏幕设置为登录屏幕。 However, I either get errors or unintended behavior when trying solutions that have worked for others.
但是,在尝试对他人有用的解决方案时,我会得到错误或意外的行为。 Below is part of my LoginForm class which is where the button is pressed.
下面是我的LoginForm类的一部分,它是按下按钮的地方。
import React, {Component} from 'react';
import { StyleSheet, View, TextInput, TouchableOpacity, Text} from 'react-native
';
export default class LoginForm extends Component {
startRegistration() {
this.props.navigation.navigate('Registration');
}
<TouchableOpacity onPress={this.startRegistration} style={style
s.buttonRegister}>
<Text style={styles.buttonText}>REGISTER</Text>
</TouchableOpacity>
When the registration button is presses it invokes the startRegistration function. 按下注册按钮时,它将调用startRegistration函数。 Here is my App.js class:
这是我的App.js类:
import React, {Component} from "react";
import {createStackNavigator, createAppContainer} from "react-navigation";
import Login from './src/components/login/Login';
import Registration from './src/components/login/Registration';
export default class FastAttendance extends Component {
render() {
return <AppContainer />
}
}
const AppNavigator = createStackNavigator({
Login: {screen: Login},
Registration: {screen: Registration},
},
{
initialRouteName: 'Login',
}
);
const AppContainer = createAppContainer(AppNavigator);
When I try to run this my login screen appears fine but when I click the registration button I get an error "undefined is not an object (evaluating 'this.props.navigation')". 当我尝试运行此命令时,我的登录屏幕显示正常,但是当我单击注册按钮时,出现错误“未定义不是对象(正在评估'this.props.navigation')”。 As suggested in other threads I have removed the this.props into this code
正如其他线程所建议的那样,我已将this.props删除到此代码
navigation.navigate('Registration');
However when I click on the registration button I get an error "Can't find variable: navigation" 但是,当我单击注册按钮时,出现错误“找不到变量:导航”
Another variation I have tried is changing my createStackNavigator function in my App.js file to this: 我尝试过的另一个变体是将App.js文件中的createStackNavigator函数更改为:
const AppNavigator = createStackNavigator({
screen: Login,
screen: Registration
});
Obviously with not initial route indicated the behavior is squirly and the app goes straight to the registration screen when starting the app but there are not errors. 显然,没有初始路线表示行为异常,并且在启动应用程序时,应用程序直接进入注册屏幕,但没有错误。 Is there something wrong with my createStackNavigator function or something else?
我的createStackNavigator函数是否存在问题或其他原因?
Instead of 代替
{this.startRegistration} {this.startRegistration}
do 做
{ () => this.startRegistration()} {()=> this.startRegistration()}
another solution is to bind the function in the constructor 另一个解决方案是将函数绑定到构造函数中
this.startRegistration.bind(this) this.startRegistration.bind(this)
您需要将导航作为道具,例如:
<MyListComponent navigation={this.props.navigation} />
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.