简体   繁体   English

如何使用堆栈导航器通过按一下按钮从一个屏幕导航到另一个屏幕?

[英]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.

相关问题 如何从一个屏幕获取数据到另一个屏幕(当前使用 Stack Navigator) - How can I get data from one screen to another (currently using Stack Navigator) 如何从功能组件导航到选项卡导航器中的屏幕,其中该选项卡导航器嵌套在父堆栈导航器中 - How to Navigate from a functional component, from a screen in a tab navigator, where that tab navigator is nested in a parent stack navigator 从Drawer-Navigator中的一个Stack-Navigator导航到同一Drawer-Navigator中的另一个Stack-Navigator - Navigate from one Stack-Navigator in Drawer-Navigator to another Stack-Navigator in same Drawer-Navigator 从嵌套堆栈导航器导航到根屏幕 - Navigate to root screen from nested stack navigator 如何使用 FlatList 从一个屏幕导航到另一个屏幕 - How to navigate from one screen with FlatList to another screen 反应导航 | 如何从嵌套在 Stack Navigator 中的子屏幕更改 Tab Navigator 上的按钮? - React Navigation | How do I change the buttons on a Tab Navigator from a child screen nested in a Stack Navigator? 如何在 React Native 中从一个屏幕导航到另一个屏幕? - How to navigate from one screen to another in React native? 我怎样才能让这些组件导航到另一个屏幕做出反应? - How can I make these components navigate to another screen in react? 当我按下导航器上的按钮图标时如何应用,模式将使用本机反应自动弹出 - How to apply when I press the button icon on the navigator the modal will automatically popup using react native 当我按下按钮时如何将数据从一个组件发送到另一个组件。 React.js - how to send the data from one component to another component when i press a button. React.js
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM