获取 undefined 不是 object(评估 '_this.props.navigation')

Getting undefined is not an object (evaluating '_this.props.navigation')

我正在使用 DrawerNavigator,我有 3 个页面:Router pagemainScreen 和一个 photos page

我做了一个 header 导航栏区域,我用这个 <TouchableHighlight onPress={() => this.props.navigation.navigate('DrawerOpen')}> 在 mainScreen 中打开抽屉菜单,并将其用于照片页面,菜单在 mainScreen 中没问题,但是当我单击 <TouchableHighlight onPress={() => this.props.navigation.navigate('DrawerOpen')}> 在照片页面,我得到错误:

undefined is not an object (evaluating '_this.props.navigation')

我该如何解决?

我的照片页面:

import React from 'react';
import { Button, ScrollView, View, Text, StyleSheet, TouchableHighlight } from 'react-native';
import { DrawerNavigator } from 'react-navigation';
import MaterialIcons from 'react-native-vector-icons/MaterialIcons';
import Icon from 'react-native-vector-icons/FontAwesome'

const MyNavScreen = ({ navigation }) => (
  <View>
    <View style={styles.containerNavbar}>
      <TouchableHighlight onPress={() => this.props.navigation.navigate('DrawerOpen')}>
        <Icon name="bars" size={30} color="#fff" />
      </TouchableHighlight>
      <Text style={styles.navbarTitle}>Photos</Text>
    </View>

    <ScrollView>
      <View><Text>photo</Text></View>
      <Button onPress={() => navigation.goBack(null)} title="Go back" />
    </ScrollView>
  </View>
);

const MyPhotosHomeScreen = ({ navigation }) => (
  <MyNavScreen navigation={navigation} />
);
MyPhotosHomeScreen.navigationOptions = {
  title: 'Photos',
  drawerIcon: ({ tintColor }) => (
    <MaterialIcons
      name="photo"
      size={24}
      style={{ color: tintColor }}
    />
  ),
};
export default MyPhotosHomeScreen;

主屏幕:

export default class MainScreen extends React.Component {
    static navigationOptions = {
        drawerLabel: 'Home',
        drawerIcon: ({ tintColor }) => (
            <MaterialIcons
                name="home"
                size={24}
                style={{ color: tintColor }}
            />
        )
    };

    render() {
        return (
            <View>
                <View style={styles.containerNavbar}>
                    <TouchableHighlight onPress={() => this.props.navigation.navigate('DrawerOpen')}>
                        <Icon name="bars" size={30} color="#fff" />
                    </TouchableHighlight>
                    <Text style={styles.navbarTitle}>mainScreen</Text>
                </View>

                <View>
                    <View style={styles.containerFooter}>
                        <Text style={styles.footerTitle}>Footer</Text>
                    </View>
                </View>
            </View>

        )
    }
}

也许我忽略了什么,但它看起来只是一个简单的 Javascript 错误。您正在破坏纯组件中的道具 MyNavScreen:

const MyNavScreen = ({ navigation }) => (

这意味着您无权访问 this.props。您只能访问解构的 prop navigation。因此,未定义错误的原因实际上是未定义的:

<TouchableHighlight onPress={() => this.props.navigation.navigate('DrawerOpen')}>

如果您将其更改为直接使用 navigation,它应该可以工作:

<TouchableHighlight onPress={() => navigation.navigate('DrawerOpen')}>

mainScreen 上,你很好,因为它不是具有解构参数的纯组件。所以您仍然可以在 render().

中访问 this.props

如果这给您带来麻烦,您应该复习一下 destructing

如果您在子元素中使用 TouchableOpacity/Height,请像这样传递 this.props.onPress

<TouchableOpacity onPress={this.props.onPress}/>

然后像这样调用父组件中的 onPress 函数:

<Parent onPress={this.Handlepress} />

我在使用 header 组件时遇到了同样的问题

现在您可以像这样在其他组件中使用导航变量

<TouchableOpacity onPress={() => { this.props.navigation.navigate("Play");}}>

编码愉快 :)

这就是我在 React Navigation 2 版本中的做法:我从 StackNavigator 调用 openDrawer() 方法,它是 DrawerNavigator.[=15 的子导航器=]

这是我的 DrawerNavigator:

export const Drawer = DrawerNavigator(
    {
        MyAccount: {screen: TabsStack},
    });


export const TabsStack = StackNavigator({

    Tabs: {
        screen: Tabs, navigationOptions: ({navigation}) => ({
            headerLeft: (
                <TouchableOpacity style={{marginLeft: 10, marginTop: 3}}
                                  onPress={() => navigation.openDrawer()}>
                    <Image source={require('./assets/menu_h.png')}/>
                </TouchableOpacity>)
        })

绑定这个对我有用

在我的例子中,当我将 this 绑定到在构造函数中调用 prop 的方法时它起作用了。

constructor(props){
    super(props);
    this.showDetails = this.showDetails.bind(this);// you should bind this to the method that call the props
}

showDetails(_id){
    this.props.navigation.navigate('Details');
}

对于功能组件,请尝试使用 Navigation 钩子进行深度导航

或者直接使用箭头函数

showDetails = (_id) => {
      this.props.navigation.navigate('Details');
}

因为当您使用表达式函数时,它会创建它自己的范围


试试这个:

import { withNavigation } from 'react-navigation';

withNavigation 在整个 project/app 提供道具,您可以从任何地方访问导航道具。

onPress={() => this.props.navigation.navigate('DrawerOpen')} 

最后,

export default withNavigation(MyPhotosHomeScreen);

看看这个 https://reactnavigation.org/docs/en/connecting-navigation-prop.html

如果您在子组件中使用导航,请不要忘记将道具中的导航发送给子组件

    <ChildComponent navigation={this.props.navigation}/>

像这样访问子组件

    props.navigation.navigate("ScreenName")

我遇到了同样的问题。我就是这样解决的:

  1. 验证你所有的构造函数都有 "props" 有参数
  2. 在构造函数中使用函数 this.props.navigation.navigate 绑定函数,如下所示:this.your_function = this.your_function.bind(this)

函数式组件以 props 作为参数。 你应该试试这个

const MyNavScreen = ({props}) =>

然后调用没有这个关键字的道具

onPress = {() => props.navigation.navigate('DrawerOpen')} 

当您在 createStackNavigator 中定义屏幕时,它默认传递一个名为 navigation 的道具, 像这样 => navigation={this.props.navigation}

但是当你使用 this.props.navigation.navigator("YOUR SCREEN ") 并且没有在 createStackNavigator 中定义此屏幕,您必须通过 navigation={this.props.navigation} 形成您在 createStackNavigator 中定义的屏幕,然后您可以在您的组件中使用它。

class ProductScreen 扩展组件 {

导出默认的 ProductScreen; // 确保在这一行底部提到

称之为

  <TouchableOpacity
        onPress = {() => this.props.navigation.navigate('ProductAddScreen')}
        activeOpacity={0.7} style={styles.button}>

       <Text style={styles.message}>{this.state.notFound} </Text>

  </TouchableOpacity>

如果你想在子组件中导航,那么你必须在子组件中获取道具。

假设您有 3 个组件 - Comp_1、Comp_2、Comp_3,并且您想要从 Comp_2 -> Comp_3 导航。为此,请按照下列步骤操作。

  1. 在Comp_1component.Like中传递道具

    <Comp_2 navigation={this.props.navigation}/>

  2. 现在 Comp_2,我们可以像这样从 Comp_2 -> Comp_3 导航。

    this.props.navigation.navigate('Comp_3');

例如 -

<Button onPress = {() => this.props.navigation.navigate('Comp_3')} title = 'Go to Comp_3 Screen' />

在您尝试进入另一个视图的组件中,您必须通过在导入组件的 onPress 中使用它来发送对象导航

例子

在视图中实现组件 <CardComponent title="bla bla" navigation={this.props.navigation} />

组件模板

<View> <Button title={this.props.title} onPress={()=> this.props.navigation.navigate("anotherAwesomeView")}/> </View>

这个问题是因为你尝试实现的组件没有在 stackNavigation 上定义,因此 methot 导航对你来说不可用,通过参数传递这个对象导航器你就可以访问它

试试这个,你可能会错过从 '@react-navigation/native' 导入 useNavigation();

import { useNavigation } from '@react-navigation/native';

function NotificationsScreen() {
const navigation = useNavigation(); 
return(
<Button
        onPress={() => navigation.navigate('Notifications')}
        title="Go to notifications"
      />
);
}

enter code here

对于那些不使用 class 组件并且更喜欢功能组件的人: 首先导入 useNavigation,然后从您希望导航的屏幕启动(声明)变量 navigation = useNavigation();

import { useNavigation } from '@react-navigation/native';

function ScreenToNavigateFrom() {
const navigation = useNavigation(); 
return(
<TouchableOpacity onPress={() => navigation.navigate('AnotherScreen')}>
   <Text style={{fontWeight: "bold", color:"white" }} >Performance</Text>
</TouchableOpacity>
);
}

此答案由 gisthub 问题提供:https://github.com/react-navigation/react-navigation/issues/7961

非常简单:按照此处的一些步骤操作

  1. 从“@react-navigation/native”导入{useNavigation};

  2. 常量首页=()=>{

//在函数作用域下添加这一行。

const navigation = useNavigation();

return(

navigation.navigate('Detail')}>

//你的代码......................

)

}