开发者问题收集

(React-Native)未定义不是对象(评估_'this.props.navigation.navigate')

2018-09-14
3665

我对 React 还是个新手。我想让我的登录按钮在按下时创建一个新屏幕。我尝试了多次,但似乎无法解决此错误:

error screen

我做错了什么?

LoginForm.js:

    import React, { Component } from 'react';
    import { TextInput, Text, View, StyleSheet, TouchableOpacity } from  'react-native';
    import { StackNavigator } from 'react-navigation';
    import { Button, CardSection, Input, Spinner } from './common';
    import Account from './screens/Account';
    import SignUpForm from './SignUpForm';

    class LoginForm extends Component {
        render() {
            return (
              <View style={styles.container}>
                <TextInput
                  placeholder="Username or email"
                  placeholderTextColor='white'
                  returnKeyType='next'
                  style={styles.input}
                  keyboardType="email-address"
                  onSubmitEditing={() => this.passwordInput.focus()}
                />

                <TextInput
                  secureTextEntry //turns text into *** good for passwords
                  label="Password"
                  placeholder="password"
                  placeholderTextColor='white'
                  secureTextEntry
                  returnKeyType='go'
                  style={styles.input}
                  ref={(input) => this.passwordInput = input}
             />
             <TouchableOpacity style={styles.buttonContainer}>
             <Text style={styles.buttonText}>LOGIN</Text>
             </TouchableOpacity>
             <Text style={styles.textStyle}> Need help logging in?{'\n'}
             or
             </Text>
             <View style={styles.divider} />
             <TouchableOpacity
             style={styles.buttonContainer}
             onPress={() => this.props.navigation.navigate('SignUpForm')}
             >
            <Text style={styles.buttonText}>Sign Up</Text>
            </TouchableOpacity>
            </View>
            );
        }
    }
    export default LoginForm;

**Account.js:**

    import React from 'react';
    import { View, Image, TouchableOpacity, Text, TextInput } from 'react-native';
    import { StackNavigator } from 'react-navigation';
    import { Card, Button, Spinner, CardSection } from '../common';
    import LoginForm from '../LoginForm';


    class Account extends React.Component {
        static navigationOptions = {
            tabBarLabel: 'Account'
        }
        render() {
            return (<View style={styles.containerStyle}>
              <Card>
                <View style={styles.logoContainer}>
                  <Image style={styles.logo} source= . 
            {require('../../Images/ShoeJackCityLogo.png')}/>
                </View>
                <View style={styles.formContainer}>
                  <LoginForm />
                </View>
              </Card>
            </View>);
        }
    }
    export default Account;

**SignUpForm.js:**

    import React, { Component } from 'react';
    import { TextInput, Text, View, StyleSheet, TouchableOpacity } from 'react-native';
    import { Button, CardSection, Input, Spinner } from './common';
    import router from '../../config/router';


    class SignUpForm extends Component {
        render() {
            return (
              <View style={styles.container}>
                <TextInput
                  placeholder="Username or email"
                  placeholderTextColor='white'
                  returnKeyType='next'
                  style={styles.input}
                  keyboardType="email-address"
                  onSubmitEditing={() => this.EmailInput.focus()}
/>
<TextInput
placeholder="Email"
placeholderTextColor='white'
returnKeyType='next'
style={styles.input}
keyboardType="email-address"
onSubmitEditing={() => this.passwordInput.focus()}
/>
<TextInput
secureTextEntry //turns text into *** good for passwords
label="Password"
placeholder="password"
placeholderTextColor='white'
secureTextEntry
returnKeyType='go'
style={styles.input}
ref={(input) => this.passwordInput = input}
/>
<TouchableOpacity style={styles.buttonContainer}>
<Text style={styles.buttonText}>Register</Text>
</TouchableOpacity>
</View>
);
}
}
export default SignUp;
import React from 'react';
import { TabNavigator, StackNavigator } from 'react-navigation';
import { Icon } from 'react-native-elements';

**Router.js**

import Tournaments from '../components/screens/Tournaments';
import Account from '../components/screens/Account';
import Artists from '../components/screens/Artists';
import Shop from '../components/screens/Shop';
import SignUpForm from '../components/SignUpForm';

export const AccountStack = StackNavigator({
  Account: {
    screen: Account,
    navigationOptions: {
    title: 'Account',
    headerBackTitle: null,
  },
  SignUpForm: {
    screen: SignUpForm,
    navigationOptions: {
      title: 'Register'
    }
  },

忽略此:jsksjkjdkfjkdfkjkdjskfjskjfjksjkjfkjsjfkjskfsjfjsjfksjfjfskfjkskjfjkjskjfjjksfjkfjkfjssfjkfksskjfjsfjk

3个回答
<LoginForm />

我没有看到您将任何 props 传递到 LoginForm 中,该属性在 onPress 中需要 this.props.navigation。

H.P.
2018-09-14

我认为您对 react-native 中的导航堆栈的工作方式有些困惑。因此,本质上有两种导航到屏幕的方式。

  1. 通过在 StackNavigator 类中声明它,您将为 <LoginForm/> 组件提供所需的导航对象。一旦声明,您将需要从任何屏幕导航到登录表单 - 例如 - this.props.navigation.navigate('LoginForm') 注意 - 您只能从 <Account/><Signup/> 导航,因为这两个屏幕当前在其范围内拥有导航对象。现在,一旦您导航到登录表单组件,您基本上就会在范围内拥有导航对象,并且您的 undefined 错误将消失。

  2. 如果您决定不在 StackNavigator 类中声明 <LoginForm/> 屏幕,则需要将 navigation 对象明确作为 props 传递给组件,例如 - <LoginForm navigation={this.props.navigation} /> 。因此,无论何时呈现此组件,您都可以以与使用相同的方式获取导航 prop。

希望它能为您清除一切 :)

Aseem Upadhyay
2018-09-15

如果没有看到您声明 stafcknavigation 的位置,我很难判断,如果没有,则此链接可了解如何 http://react-navigation%20stack%20navigator%20custom%20header

否则,您可能没有将导航方法传递给嵌套组件 这里有一个链接可以查看如何执行此操作 在 React Native 中在组件之间导航

Walter Shub
2018-09-14