简体   繁体   English

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

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

I'm trying to create a React Native app with some basic routing. 我正在尝试使用一些基本的路由创建一个React Native应用程序。

This is my code so far: 到目前为止,这是我的代码:

App.js: App.js:

import React from 'react'
import { StackNavigator } from 'react-navigation'
import MainScreen from './classes/MainScreen'


const AppNavigator = StackNavigator(
    {
        Index: {
            screen: MainScreen,
        },
    },
    {
        initialRouteName: 'Index',
        headerMode: 'none'
    }
);

export default () => <AppNavigator />

MainScreen.js: MainScreen.js:

import React, { Component } from 'react'
import { StyleSheet, Text, View, Button, TouchableOpacity, Image } from 'react-native'
import HomeButton from './HomeButton'

export default class MainScreen extends Component {
    static navigatorOptions = {
        title: 'MyApp'
    }

    constructor(props) {
        super(props)
    }

    render() {
        return (
            <View style={styles.container}>
                <Image source={require('../img/logo.png')} style={{marginBottom: 20}} />
                <HomeButton text='Try me out' classType='first' />
            </View>
        )
    }
}

const styles = StyleSheet.create({
    container: {
        flex: 1,
        backgroundColor: '#fff',
        alignItems: 'center',
        justifyContent: 'center'
    }
})

HomeButton.js: HomeButton.js:

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


export default class HomeButton extends Component {
    constructor(props) {
        super(props)
    }

    render() {
        return (
            <TouchableOpacity
                onPress={() => navigate('Home')}
                style={[baseStyle.buttons, styles[this.props.classType].style]}
            >
                <Text style={baseStyle.buttonsText}>{this.props.text.toUpperCase()}</Text>
            </TouchableOpacity>
        )
    }
}

var Dimensions = require('Dimensions')
var windowWidth = Dimensions.get('window').width;

const baseStyle = StyleSheet.create({
    buttons: {
        backgroundColor: '#ccc',
        borderRadius: 2,
        width: windowWidth * 0.8,
        height: 50,
        shadowOffset: {width: 0, height: 2 },
        shadowOpacity: 0.26,
        shadowRadius: 5,
        shadowColor: '#000000',
        marginTop: 5,
        marginBottom: 5
    },
    buttonsText: {
        fontSize: 20,
        lineHeight: 50,
        textAlign: 'center',
        color: '#fff'
    }
})

const styles = {
    first: StyleSheet.create({
        style: { backgroundColor: '#4caf50' }
    })
}

Everything works fine, but when pressing the button I get 一切正常,但是当我按下按钮时

Can't find variable: navigate 找不到变量:导航

I've read that I have to declare it like this: 我读过我必须这样声明:

const { navigate } = this.props.navigation

So I edited HomeButton.js and added that line at the beginning of the render function: 因此,我编辑了HomeButton.js并在render函数的开头添加了这一行:

render() {
    const { navigate } = this.props.navigation
    return (
        <TouchableOpacity
            onPress={() => navigate('Home')}
            style={[baseStyle.buttons, styles[this.props.classType].style]}
        >
            <Text style={baseStyle.buttonsText}>{this.props.text.toUpperCase()}</Text>
        </TouchableOpacity>
    )
}

Now I get: 现在我得到:

TypeError: undefined is not an object (evaluating 'this.props.navigation.navigate') TypeError:undefined不是对象(评估'this.props.navigation.navigate')

It seems that the navigation object is not coming into the properties, but I don't understand where should I get it from. 似乎navigation对象没有进入属性,但是我不知道应该从哪里获取它。

What am I missing here? 我在这里想念什么?

React-navigation pass navigation prop to the screen components defined in the stack navigator. React-navigation将导航道具传递给在堆栈导航器中定义的屏幕组件。

So in your case, MainScreen can access this.props.navigation but HomeButton can't. 因此,在您的情况下, MainScreen可以访问this.props.navigationHomeButton不能。

It should work if you pass navigation prop from MainScreen to HomeButton : 如果您将导航道具从MainScreen传递到HomeButton它应该可以工作:

<HomeButton text='Try me out' classType='first' navigation={this.props.navigation}/>

Edit: You have to define the Home screen in your stack navigator in order to navigate to it, your onPress={() => navigate('Home')} won't work until then. 编辑:您必须在堆栈导航器中定义Home屏幕才能导航到它,您的onPress={() => navigate('Home')}直到那时才起作用。

暂无
暂无

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

相关问题 undefined不是对象(评估this.props.navigation) - undefined is not an object (Evaluating this.props.navigation) 未定义不是对象(评估“ 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