简体   繁体   English

未定义不是对象(评估“ this.props.navigation”)

[英]undefined is not an object (evaluating “this.props.navigation”)

I'm trying to create basic StackNavigator 我正在尝试创建基本的StackNavigator

This is how my code looks like: 这是我的代码的样子:

App.js App.js

import React, {
    Component
} from 'react';
import {
    StackNavigator
} from 'react-navigation';
import Login from './src/screens/Login';
import Secured from './src/screens/Secured';
import Register from './src/screens/Register';

const Navigation = StackNavigator({
    Login: {
        screen: Login,
    },
    Register: {
        screen: Register,
    },
    Secured: {
        screen: Secured,
    }
})
export default Navigation;

Login.js Login.js

import React, {
    Component
} from "react";
import {
    StyleSheet,
    Text,
    TextInput,
    View,
    Button,
    TouchableOpacity
} from "react-native";
import {
    navigation
} from 'react-navigation';

const styles = StyleSheet.create({
    container: {
        flex: 1,
        justifyContent: "center",
        alignItems: "center",
        backgroundColor: "#FFF5F5",
    },
    texts: {
        height: 150,
    },
    signupText: {
        color: '#000000',
        fontSize: 14
    },
    signupButton: {
        color: '#ff0000',
        fontSize: 16,
        fontWeight: '500'
    },
    loginButton: {
        fontSize: 24
    },
    loginTextCont: {
        flexGrow: 1,
        justifyContent: 'center',
        alignItems: 'center'
    },
    signupTextCont: {
        flexGrow: 1,
        alignItems: 'flex-end',
        justifyContent: 'center',
        paddingVertical: 16,
        flexDirection: 'row'
    },
});

export default class Login extends Component {
    static navigationOptions = {
        title: "Login",
    };
    render() {
        return ( <
            View style = {
                styles.container
            } >
            <
            View style = {
                styles.loginTextCont
            } >
            <
            Text style = {
                {
                    fontSize: 36,
                    fontFamily: "Futura"
                }
            } >
            Willkommen zu < /Text> <Text style={{fontSize: 36, fontFamily: "Futura", color:'#ff0000' }}>LifeStorm!</Text >
            <
            View style = {
                {
                    width: 50,
                    height: 20
                }
            }
            /> <
            TextInput placeholder = "Benutzer Name" / >
            <
            View style = {
                {
                    width: 50,
                    height: 3
                }
            }
            /> <
            TextInput placeholder = "Passwort" / >
            <
            View style = {
                {
                    width: 50,
                    height: 10
                }
            }
            /> <
            TouchableOpacity onPress = {
                () => this.props.navigation.navigate("Register")
            } > < Text style = {
                styles.loginButton
            } > Weiter < /Text></TouchableOpacity >
            <
            /View> <
            View style = {
                styles.signupTextCont
            } >
            <
            Text style = {
                styles.signupText
            } >
            Haben Sie kein Konto ?
            <
            /Text> <
            TouchableOpacity onPress = {
                this.signup
            } > < Text style = {
                styles.signupButton
            } > Sich anmelden < /Text></TouchableOpacity >
            <
            /View> <
            /View>
        );
    }
}

Register.js Register.js

import React, {
    Component
} from 'react';
import {
    StyleSheet,
    Text,
    View,
    StatusBar,
    TouchableOpacity
} from 'react-native';
import {
    navigation
} from 'react-navigation';

import Form from '../forms/Form';

export default class Register extends Component < {} > {
    static navigationOptions = {
        title: "Register",
    };

    render() {
        return ( <
            View style = {
                styles.container
            } >
            <
            Logo / >
            <
            Form type = "Signup" / >
            <
            View style = {
                styles.signupTextCont
            } >
            <
            Text style = {
                styles.signupText
            } > Already have an account ? < /Text> <
            TouchableOpacity onPress = {
                () => navigate("Login")
            } > < Text style = {
                styles.signupButton
            } > Sign in < /Text></TouchableOpacity >
            <
            /View> <
            /View>
        )
    }
}

const styles = StyleSheet.create({
    container: {
        backgroundColor: '#455a64',
        flex: 1,
        alignItems: 'center',
        justifyContent: 'center'
    },
    signupTextCont: {
        flexGrow: 1,
        alignItems: 'flex-end',
        justifyContent: 'center',
        paddingVertical: 16,
        flexDirection: 'row'
    },
    signupText: {
        color: 'rgba(255,255,255,0.6)',
        fontSize: 16
    },
    signupButton: {
        color: '#ffffff',
        fontSize: 16,
        fontWeight: '500'
    }
});

module.exports = Register;

Every time when I run my app on iPhone through Expo I have the same mistake: 每当我通过Expo在iPhone上运行我的应用程序时,都会遇到相同的错误:

undefined is not an object(evaluating "this.props.navigation") 未定义不是对象(评估“ this.props.navigation”)

If I understood properly, then the mistake is in Login.Js 如果我理解正确,那么错误在于Login.Js

I've tried already many different solutions, but nothing worked. 我已经尝试了许多不同的解决方案,但是没有任何效果。

Where is the mistake? 错误在哪里?

1.There is no need to import "navigation" on every screen to use it.Remove the line import { navigation } from 'react-navigation'; 1.无需在每个屏幕上导入“导航”即可使用它。从“ react-navigation”中删除行import {navigation}; on both screens. 在两个屏幕上。

2.In Register.js file you are using 2,在Register.js文件中使用
TouchableOpacity onPress = {() => navigate("Login")} have you declared it anywhere as described in official docs: https://facebook.github.io/react-native/docs/navigation.html TouchableOpacity onPress = {()=> navigation(“ Login”)}是否已按照官方文档中的说明在任何地方声明了它: https : //facebook.github.io/react-native/docs/navigation.html

The first step is to install react-navigation in your project as follows 第一步是在您的项目中安装react-navigation,如下所示
npm install --save react-navigation npm install-保存反应导航

No need to import navigation, instead you have to add constructor and use below code 无需导入导航,而是必须添加构造函数并使用以下代码
constructor(props) { 构造函数(道具){
super(props); 超级(道具);
} }
Also add below code as follows 还添加以下代码,如下所示
render() { render(){
const { navigate } = this.props.navigation; const {navigation} = this.props.navigation;
} }
use "navigate('Login')" to navigate to login screen 使用“ navigate('Login')”导航到登录屏幕

暂无
暂无

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

相关问题 未定义不是对象(评估“ this.props.navigation”) - undefined is not an object (evaluating 'this.props.navigation') undefined不是对象(评估this.props.navigation) - undefined is not an object (Evaluating this.props.navigation) undefined 不是带有 fetch 的 object(评估“this.props.navigation”) - undefined is not an object (evaluating 'this.props.navigation') with fetch 未定义不是对象(评估“ this.props.navigation”) - undefined is not an object (evaluating 'this.props.navigation") undefined 不是一个对象(评估&#39;this.props.navigation&#39;)-React Native - undefined is not an object (evaluating 'this.props.navigation') - React Native TypeError:undefined 不是 React Native 中的对象(评估“_this.props.navigation”) - TypeError: undefined is not an object (evaluating '_this.props.navigation') in React Native 如何修复“未定义不是 object(评估 'this.props.navigation')” - How to fix “undefined is not an object (evaluating 'this.props.navigation')” undefined不是评估_this.props.navigation React Native的对象 - undefined is not an object evaluating _this.props.navigation React Native 未定义不是对象(评估&#39;_this.props.navigation&#39;) - Undifined is not an object(evaluating '_this.props.navigation') 如何解决React Native中的导航问题(TypeError:undefined不是一个对象(评估&#39;_this.props.navigation)) - How to fix navigation problem in React Native (TypeError: undefined is not an object (evaluating '_this.props.navigation)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM