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

时间:2020-06-11 00:51:37

标签: javascript reactjs react-native

我正在为我的家人开发这个应用程序,到目前为止,我已经启动并运行了所有程序,但是现在我面临着这个问题,因为我想使用onPress()加载另一个视图,但是我不能弄清楚我的错误是什么。

这是我的代码

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);

基本上,每次我按下按钮时,我都希望函数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]

关于我在做什么错的任何想法,或者任何人都可以指出我该怎么做。

1 个答案:

答案 0 :(得分:1)

从导出中删除props。道具将通过react自动传递到组件中。这里的问题是您正在使用道具调用LoginScreen,而道具实际上不在LoginScreen范围之外

所以有export default LoginScreen

另外,将props添加到LoginScreen的函数定义中,使其变为:const LoginScreen = (props) => { ... }

然后在实例化登录屏幕的任何地方使用<LoginScreen **ADD ANY PROPS YOU WANT TO ADD HERE** />

相关问题