0

我有一个包含整个导航堆栈的文件。在我的导航标题中,我有一个菜单,我想打开一个抽屉。现在在这个例子中我得到了错误:Cannot read property 'navigation' of undefined

我的 AppNavigation 文件:

import React from 'react';
import { Text } from 'react-native';
import { createStackNavigator, createDrawerNavigator } from 'react-navigation';
import  Login from '../components/Login';
import Dashboard from '../components/Dashboard';
import NewNotification from '../components/NewNotification';


const GuestStack = createStackNavigator(
  {
    loginScreen: { screen: Login },
  }, { 
    headerMode: 'float',
    headerLayoutPreset: 'center',
    navigationOptions: {
      headerStyle: { backgroundColor: '#61b1cd' },
      title: 'Welcome',
      headerTintColor: 'black',
    },
  },
);

const LoggedinStack = createDrawerNavigator({
  dashboard: { screen: Dashboard },
  newNotifciation: { screen: NewNotification },
});

const LoggedinNavigation = createStackNavigator(
  {
    LoggedinStack: { screen: LoggedinStack },
  }, {
    headerMode: 'float',
    navigationOptions: {
      headerStyle: { backgroundColor: '#61b1cd' },
      title: 'Welkom!',
      headerTintColor: 'black',
      headerLeft: <Text onPress = { () =>
        this.props.navigation.openDrawer('dashboard')
//  navigation.openDrawer('dashboard')
       }>Menu</Text>,
    },
  },
);

const VveNavigator = createStackNavigator(
  {
    guestStack: {
      screen: GuestStack,
    },
    loggedinStack: {
      screen: LoggedinNavigation,
    },
  }, {
    headerMode: 'none',
    initialRouteName: 'guestStack',
  },
);

export default AppNavigator;

问题似乎在这里结束了:

headerLeft: <Text onPress = { () =>
            this.props.navigation.openDrawer('dashboard')
    //  navigation.openDrawer('dashboard')
           }>Menu</Text>,

然后在我的 App.js 我有

export default class App extends React.Component {
  render() {
    return (
      <APPNavigator />
    );
  }
}

反应导航的版本是 2.18.1

谢谢

4

1 回答 1

1

headerLeft没有收到navigation道具(检查源代码)。因此,如果您想在按下时使用导航道具,您应该考虑重构您的堆栈导航器配置:

const LoggedinNavigation = createStackNavigator(
  {
    LoggedinStack: { screen: LoggedinStack },
  }, {
    headerMode: 'float',
    navigationOptions: ({ navigation }) => ({ // here you get the navigation
      headerStyle: { backgroundColor: '#61b1cd' },
      title: 'Welkom!',
      headerTintColor: 'black',
      headerLeft: (
        <Text
          onPress={() => {
            navigation.openDrawer()
          }}
        >
          Menu
        </Text>
      ),
    }),
  },
);

检查此问题以获取更多选项。

于 2018-11-09T10:05:13.013 回答