简体   繁体   English

React-Native:无法导航到第二个屏幕

[英]React-Native: Can't Navigate to the Second Screen

sorry if my question is a bit long but this is my first one here and I'm really desperate to know what's wrong with my code.抱歉,如果我的问题有点长,但这是我的第一个问题,我真的很想知道我的代码有什么问题。 Thank you in advance先感谢您

Please I need some help with my React-Native app.我的 React-Native 应用程序需要一些帮助。 So in short, my app is about a login screen (username and password) with a checkbox to save login and a login button.所以简而言之,我的应用程序是关于一个登录屏幕(用户名和密码),带有一个用于保存登录的复选框和一个登录按钮。 After entering the username and password the second screen should be a simple Welcoming screen: "Welcome {userName}".输入用户名和密码后,第二个屏幕应该是一个简单的欢迎屏幕:“欢迎 {userName}”。 If the user checks the save login box and presses login, the app will navigate to the second screen and he can't go back (no back button) and the next time the user opens the app it directly takes him/her to the second screen.如果用户选中保存登录框并按下登录,应用程序将导航到第二个屏幕,他不能 go 返回(没有返回按钮)并且下次用户打开应用程序时直接将他/她带到第二个屏幕屏幕。

So, my problem is that I can't navigate to the second screen and I don't know how to remove the back button.所以,我的问题是我无法导航到第二个屏幕,而且我不知道如何删除后退按钮。 Here is my code:这是我的代码:

App.js:应用程序.js:

 import React, { Component } from 'react'; import { SafeAreaView,Text,Button } from 'react-native'; import { createStackNavigator } from 'react-navigation-stack'; import { createAppContainer } from "react-navigation"; import FirstScreen from './first'; import SecondScreen from './second'; import myDB from './myDB'; const AppNavigator = createStackNavigator({ Login: FirstScreen, Welcome: SecondScreen }, { initialRouteName: "Login" }) const AppContainer = createAppContainer(AppNavigator) export default class App extends React.Component{ render() { return<AppContainer/>; } }

first.js:第一个.js:

 import React, { Component } from 'react'; import { SafeAreaView, Text, Button, TextInput, CheckBox, StyleSheet } from 'react-native'; import { createAppContainer } from 'react-navigation'; import { createStackNavigator } from 'react-navigation-stack'; import myDB from './myDB'; class FirstScreen extends Component { constructor (props) { super(props); } state = { userName: '', password: '', chkValue: false, }; handleUserName = (text) => { this.setState({ userName: text }); }; handlePasword = (text1) => { this.setState({ password: text1 }); }; openSecondScreen = () => { myDB.getData.userName = this.state.userName; myDB.getData.password = this.state.password; this.props.navigation.navigate('second'); }; chkChange = (text2) => { this.setState({ chkValue: text2 }); }; render() { return ( <SafeAreaView style={{ alignItems: 'center' }}> <Text>Username</Text> <TextInput style={styles.input} value={this.state.userName} placeholderTextColor="white" onChangeText={this.handleUserName} /> <Text>Password</Text> <TextInput style={styles.input} value={this.state.password} placeholderTextColor="white" onChangeText={this.handlePasword} secureTextEntry="true" /> <SafeAreaView style={styles.checkboxContainer}> <CheckBox value={this.state.chkValue} onValueChange={this.chkChange} style={styles.checkbox} /> <Text style={styles.label}>Save Login</Text> </SafeAreaView> <SafeAreaView style={styles.view1}> <Button style={styles.btn1} title="Login" onPress={this.openSecondScreen}></Button> </SafeAreaView> </SafeAreaView> ); } } export default FirstScreen; const styles = StyleSheet.create({ input: { height: 30, backgroundColor: 'white', paddingLeft: 15, paddingRight: 15, margin: 8, }, view1: { flexDirection: 'column', }, checkboxContainer: { flexDirection: 'row', marginBottom: 20, }, checkbox: { alignSelf: 'center', }, label: { margin: 8, }, });

second.js:第二个.js:

 import React, { Component } from 'react'; import { SafeAreaView, Text, Button } from 'react-native'; import myDB from './myDB'; class SecondScreen extends Component { state = { userName: myDB.getData.userName, password: myDB.getData.password }; render() { const { navigation } = this.props; const userName = navigation.getParam('userName', 'No-User'); const password = navigation.getParam('password', 'No-User'); return ( <SafeAreaView style={{ alignItems: 'center' }}> <Text>Welcome {this.state.userName}</Text> </SafeAreaView> ); } } export default SecondScreen;

myDB.js: myDB.js:

 import React, { Component } from 'react'; class myDb extends Component { static myVariable = { userName:'', password:'' }; static getData = (myVariable) => { return myVariable; }; } export default myDb;

I use snack.expo.io to build these apps just in case if it helps.我使用snack.expo.io 来构建这些应用程序,以防万一。

Have a try with the below small change试试下面的小改动

this.props.navigation.navigate('second');

to

this.props.navigation.navigate('Welcome');

as we have to use the key we have specified in the navigator to navigate to a specific screen.因为我们必须使用我们在导航器中指定的键来导航到特定屏幕。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM