HeaderLeft onPress打开抽屉,未定义不是对象

时间:2019-02-06 11:30:53

标签: react-native react-native-android

我的代码行特别是headerLeft onPress出现问题。我想在按下该图标的位置打开一个图标,以打开我的简单应用程序的抽屉导航。

这是我的AppNavigation.js

//DRAWER NAVIGATOR
const drawerNav = createDrawerNavigator({
  JobFeed: {
screen: MainScreen,
navigationOptions: {drawerLabel: 'Job Feed',}
  },
},
{
  drawerPosition : "left",   contentComponent: CustomDrawerComponent,
});

// Manifest of possible screens
const primaryNav = createStackNavigator({
  LaunchScreen: { 
screen: LaunchScreen,
navigationOptions: {
  title: "Ty, Next",
  headerTitleStyle: {
    textAlign: 'center',
    flex: 1,
    fontFamily: 'CoreSansD65Heavy',
    color: Colors.semiGray,
  }
}
   },

MainScreen: {
        screen: MainScreen,
        navigationOptions: {
          title: "Ty, Next",
          headerTitleStyle: {
            textAlign: 'center',
            flex: 1,
            fontFamily: 'CoreSansD65Heavy',
            color: Colors.semiGray,
            marginBottom: 20,
      }
    }
  },
}, 
{
  // Default config for all screens
  initialRouteName: 'MainScreen',
})

所以这就是问题所在。在我的MainScreen.js中,每当我按下按钮时,我都会放置这段代码,这是说undefined不是对象(评估_this2.props.navigation)。请帮助我,我在这个特殊问题上仍然处于困境。

static navigationOptions = ({ navigation }) => {
    const { state } = navigation;
    const {} = state;
    return {
        headerStyle:{
            backgroundColor: "Transparent",
            marginRight: 20,
            marginLeft: 20,
        }, 
        headerLeft: (
            <TouchableOpacity onPress={this.props.navigation.openDrawer()}>
                    <Icon name="bars" color={Colors.red} size={30}/>
            </TouchableOpacity>
        ),
        headerLeftStyle: styles.drawerIcon,
        headerRight: (
            <TouchableOpacity>
                    <Icon2 name="sc-telegram" color={Colors.red} size={30} />
            </TouchableOpacity>
        ),
        headerRightStyle: styles.planeIcon,
        headerTransparent:  true,
    };
}

向右滑动时,我什至无法访问抽屉导航器。任何想法为什么会这样?

1 个答案:

答案 0 :(得分:0)

在mainscreen.js中,如果您尝试创建胖箭头函数而不是直接调用onpress prop并从该函数调用,并且您的组件不是基于类的,则它是基于类的组件。 !