我想从一个屏幕导航到另一个屏幕,但我有 undefined is not an object(评估'this.props.navigation.navigate')

Posted

技术标签:

【中文标题】我想从一个屏幕导航到另一个屏幕,但我有 undefined is not an object(评估\'this.props.navigation.navigate\')【英文标题】:I want to navigate from one screen to another screen but I am having undefined is not an object (evaluating 'this.props.navigation.navigate')我想从一个屏幕导航到另一个屏幕,但我有 undefined is not an object(评估'this.props.navigation.navigate') 【发布时间】:2020-02-23 16:19:30 【问题描述】:

我想从导航抽屉的一个屏幕导航到另一个屏幕,但我遇到了错误

类型错误:未定义不是对象(评估“this.props.navigation.navigate”)

不知道为什么?

这是我的代码:

import React,  useState  from 'react';
import  View  from 'react-native';
import  Drawer, Appbar, Divider  from 'react-native-paper';
import style from './menu.style';
import popularposts from './popularposts';

export default Menu = (onMenuToggle, options, navigation) => 
    const [active, setActive] = useState('home');
    const navigate = this.props.navigation;
    return (
       <View style=style.container>
       <Appbar.Header>
            <Appbar.Content></Appbar.Content>
            <Appbar.Action icon="close" onPress=onMenuToggle />
        </Appbar.Header>
            <Drawer.Section>
             <Drawer.Item
                label="Popular Post"
                active=active === 'first'
                onPress=() => navigate('popularposts')
                />
                <Drawer.Item
                label="Important Dates/ Events"
                active=active === 'second'
                onPress=() =>  setActive('second'); 
                />
                <Divider />
                <Drawer.Item
                label="SIGN OUT"
                active=active === 'fourth'
                onPress=() =>  setActive('fourth'); 
                />
            </Drawer.Section>
       </View>
    )

【问题讨论】:

【参考方案1】:

使用

const navigate = navigation;

而不是

const navigate = this.props.navigation;

【讨论】:

我正在使用 navigation.navigate('screen Name') 并且效果很好

以上是关于我想从一个屏幕导航到另一个屏幕,但我有 undefined is not an object(评估'this.props.navigation.navigate')的主要内容,如果未能解决你的问题,请参考以下文章

Flutter:从 StreamBuilder 构建器回调内部导航到另一个屏幕

React-native-splashscreen 和导航

将数据从一个屏幕传输到另一个屏幕[重复]

如何在 React Native 中从一个屏幕导航到另一个屏幕?

在导航到另一个屏幕时继续录制音频

使用 swiftUI 中的按钮导航到新屏幕