无法在this.props.navigation.dispatch()中读取未定义的属性'dispatch'

问题描述 投票:1回答:1

我必须切换抽屉,然后创建一个DrawerComponent,其中包含我需要的所有项目,我使用NavigationActions创建一个函数来调度我的路线,但它不起作用。当我点击任何项目时我有这个错误:无法读取undefine属性'dispatch'

import {NavigationActions} from 'react-navigation';
...
class DrawerComponent extends React.Component {

  navigateToScreen = (route) => () => {
    const navigateAction = NavigationActions.navigate({
      routeName: route
    });
    this.props.navigation.dispatch(navigateAction);
}


  render()...
...
...

  <TouchableOpacity
   style={Styles.centerContent}
   onPress= {this.navigateToScreen('Messages')}

  >
   <IconIonic name="ios-mail" size={60} color= '#fff'/>
   <View>
    <Text>messages</Text>
   </View>
  </TouchableOpacity>

在抽屉里

import { createStackNavigator, createDrawerNavigator, createAppContainer } from 'react-navigation';
...
const DrawerNavigation = createDrawerNavigator({
    Home: { // entree (route name) : on peut la nommer comme on veut mais on prefere lui donner le meme nom que notre screen qu'on va afficher
      screen: HomeStackNavigation, // le screen qu'on va afficher IL DOIT ETRE UN STACK
    },
    Message: {
      screen: Messages,
    },
},
{
  drawerWidth: width*0.83,
  contentComponent: props => 
  {
    return(<DrawerComponent/>)
  },
  drawerPosition: 'left',
},
);

在那个错误之前,我有类似的东西:undefined不是评估_this.props.navigation.dispatch()的对象

react-native react-navigation
1个回答
0
投票
{
  contentComponent: props => {
    return <DrawerComponent {...props} />;
  };
}

不要吞下props。将它传递给子组件

© www.soinside.com 2019 - 2024. All rights reserved.