简体   繁体   English

如何在 React Native 上的屏幕之间导航?

[英]How to navigate between screen on React Native?

I'm working on this app for my family and so far i got everything up and running, but now I'm facing this issue because I want to load another view with onPress() , but I can't figure what my error is.我正在为我的家人开发这个应用程序,到目前为止我已经启动并运行了所有东西,但现在我遇到了这个问题,因为我想用onPress()加载另一个视图,但我不知道我的错误是什么.

This is my code这是我的代码

import React, {useState} from 'react';
import {View, StyleSheet} from 'react-native';
import {Text} from 'react-native-paper';
import {TextInput} from 'react-native-paper';
import AsyncStorage from '@react-native-community/async-storage';
import {Button} from 'react-native-paper';

const LoginScreen = () => {
  const [userId, setUserId] = useState('');
  const [loading, setLoading] = useState(false);

  const onLogin = async () => {
    setLoading(true);
    try {
      await AsyncStorage.setItem('userId', userId);
      setLoading(false);
      this.navigation.push('Call');
    } catch (err) {
      console.log('Error', err);
      setLoading(false);
    }
  };

  return (
    <View style={styles.root}>
      <View style={styles.content}>
        <Text style={styles.heading}>Enter your name</Text>
        <TextInput
          label="Name"
          onChangeText={text => setUserId(text)}
          mode="outlined"
          style={styles.input}
        />

        <Button
          mode="contained"
          onPress={onLogin}
          loading={loading}
          style={styles.btn}
          contentStyle={styles.btnContent}
          disabled={userId.length === 0}>
         Connect
        </Button>
      </View>
    </View>
  );
}


const styles = StyleSheet.create({
  root: {
    backgroundColor: '#fff',
    flex: 1,
    justifyContent: 'center',
  },
  content: {
    paddingHorizontal: 20,
    justifyContent: 'center',
  },
  heading: {
    fontSize: 18,
    marginBottom: 10,
    fontWeight: '600',
  },
  input: {
    height: 60,
    marginBottom: 10,
  },
  btn: {
    height: 60,
    alignItems: 'stretch',
    justifyContent: 'center',
    fontSize: 18,
  },
  btnContent: {
    alignItems: 'center',
    justifyContent: 'center',
    height: 60,
  },
});
export default LoginScreen(props);

Basically, everytime I press the button, I want my function onLogin() to navigate to my other file and load, but this is What I get.基本上,每次我按下按钮时,我都希望我的 function onLogin()导航到我的其他文件并加载,但这就是我得到的。

[Wed Jun 10 2020 19:32:41.934]  LOG      Error [ReferenceError: Can't find variable: props]
[Wed Jun 10 2020 19:32:42.951]  LOG      Error [ReferenceError: Can't find variable: props]
[Wed Jun 10 2020 19:32:46.182]  LOG      Error [ReferenceError: Can't find variable: props]
[Wed Jun 10 2020 19:32:46.517]  LOG      Error [ReferenceError: Can't find variable: props]
[Wed Jun 10 2020 19:32:46.820]  LOG      Error [ReferenceError: Can't find variable: props]
[Wed Jun 10 2020 19:32:47.733]  LOG      Error [ReferenceError: Can't find variable: props]
[Wed Jun 10 2020 19:32:48.500]  LOG      Error [ReferenceError: Can't find variable: props]
[Wed Jun 10 2020 19:33:02.840]  LOG      Error [ReferenceError: Can't find variable: props]
[Wed Jun 10 2020 19:33:05.786]  LOG      Error [ReferenceError: Can't find variable: props]

Any ideas on what I'm doing wrong or can anyone point me out on where to go with this.关于我做错了什么的任何想法,或者任何人都可以指出我在哪里使用这个 go。

Remove props from the export.从导出中删除props props will be passed automatically into the component by react. props 将通过 react 自动传递到组件中。 The issue here is that you are calling LoginScreen with props, and props doesn't actually exist outside of the scope of LoginScreen这里的问题是您正在使用道具调用LoginScreen ,而道具实际上并不存在于LoginScreen的 scope 之外

So instead have export default LoginScreen所以改为export default LoginScreen

Additionally, add props to the function definition of LoginScreen so it becomes: const LoginScreen = (props) => {... }此外,将props添加到LoginScreen的 function 定义中,使其变为: const LoginScreen = (props) => {... }

Then wherever you have the login screen instantiated use <LoginScreen **ADD ANY PROPS YOU WANT TO ADD HERE** />然后无论您在哪里实例化登录屏幕,请使用<LoginScreen **ADD ANY PROPS YOU WANT TO ADD HERE** />

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

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