简体   繁体   English

类型错误:无法读取未定义的属性“导航”-反应本机

[英]TypeError: Cannot read property 'navigate' of undefined - React native

I'm using Firebase with react-native to authenticate users.我正在使用 Firebase 和 react-native 来验证用户。 My main file is App.js and I'm sending the users to Login component and I have two components to manage routes.我的主文件是 App.js,我将用户发送到 Login 组件,我有两个组件来管理路由。 Appnavigator.js for creating a switchNavigator and DrawerNavigator.js for createDrawernavigator. Appnavigator.js 用于创建 switchNavigator 和 DrawerNavigator.js 用于 createDrawernavigator。 After authentication, I want to send the user to one inner component (Admin).身份验证后,我想将用户发送到一个内部组件(管理员)。

Following are the codes for particular pages,以下是特定页面的代码,

Login.js登录.js

export default class Login extends React.Component {

  state = {
    email:"",
    password: "",
    errorMessage: null
  }

  handleLogin = () => {
      const { email , password } = this.state;
      firebase.auth()
          .signInWithEmailAndPassword(email, password)
          .then(() => this.props.navigation.navigate('Admin'))
          .catch(error => alert(error))
    } 
  render(){
.........................


<TouchableOpacity style={styles.loginBtn}  onPress = {
          this.handleLogin
        }>

And I have two components to manage login activities.我有两个组件来管理登录活动。 DrawerNavigator.js DrawerNavigator.js

import Admin from '../screens/Admin.js';
import Login from '../screens/Login.js'

import menu from '../assets/drawer.png';

const DrawerNavigator = createDrawerNavigator({

    Admin: Admin,
},
{
    drawerOpenRoute: 'openDrawer',
    drawerCloseRoute: 'DrawerClose',
    drawerToggleRoute: 'DrawerToggle'
},
);

export default DrawerNavigator;

And I have AppNavigator page to create an AppContainer and switchnavigator.我有 AppNavigator 页面来创建一个 AppContainer 和 switchnavigator。

import React from 'react';
import { createAppContainer, createSwitchNavigator } from 'react-navigation';

import DrawerNavigator from './DrawerNavigator.js';
import Login from '../screens/Login.js';
import App from '../App.js';


export default createAppContainer(
    createSwitchNavigator({
        Main:DrawerNavigator,
    })
);

I have used this.props.navigation.navigate('Admin') In the handleLogin function to route the user after a successful login.我已经使用 this.props.navigation.navigate('Admin') 在 handleLogin 函数中成功登录后路由用户。 But I'm getting an error但我收到一个错误

TypeError: Cannot read property 'navigate' of undefined

App.js file App.js 文件

import React from 'react';
import { StyleSheet, Platform, Text, View, Image, TextInput, TouchableOpacity } from 'react-native';import AppNavigator from './navigation/AppNavigator.js';
import Login from './screens/Login.js';
import AppNavigator from '../navigation/AppNavigator.js;
export default class App extends React.Component {

  render(){
  return (

    <Login />
<AppNavigator />
  );
}}

You have not added your Login component as a part of AppNavigator :您尚未将Login组件添加为AppNavigator的一部分:

export default createAppContainer(
    createSwitchNavigator({
        Main:DrawerNavigator,
        Auth: createStackNavigator(
          {
            Login: Login
          }
      )
    })
);

暂无
暂无

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

相关问题 React Native 无法读取未定义的属性“导航” - React Native Cannot read property 'navigate' of undefined 无法读取未定义的属性“导航” - React Native Navigation - Cannot read property 'navigate' of undefined - React Native Navigation React-native:无法读取未定义的属性“ navigate” - React-native:cannot read property 'navigate' of undefined React Native TabNavigator,无法读取未定义的属性“ navigate” - React Native TabNavigator, Cannot read property 'navigate' of undefined React Native jest test:TypeError:无法读取未定义的属性&#39;unsubscribeFromTopic&#39; - React Native jest test: TypeError: Cannot read property 'unsubscribeFromTopic' of undefined React Native + Fetch =&gt; TypeError:无法读取未定义的属性“ then” - React Native + Fetch => TypeError: Cannot read property 'then' of undefined 反应本机TypeError:无法读取未定义的属性“导航” - react native TypeError: Cannot read property 'navigation' of undefined TypeError:无法在React Native中读取未定义的属性&#39;receiptnumber&#39; - TypeError: Cannot read property 'receiptnumber' of undefined in react native React Native ListView-TypeError:无法读取未定义的属性&#39;cloneWithRows&#39;(…) - React Native ListView - TypeError: Cannot read property 'cloneWithRows' of undefined (…) TypeError:无法读取未定义的属性“数量”。 反应原生 Redux - TypeError: Cannot read property 'qty' of undefined. React Native Redux
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM