简体   繁体   English

如何在React Native中切换抽屉式导航器?

[英]How to toggle drawer navigator in react native?

I am using drawer navigation. 我正在使用抽屉式导航。 When I click on the hamburger menu icon the drawer of the drawer navigation is not working, I am not able to toggle the drawer. 当我单击汉堡菜单图标时,抽屉导航的抽屉不起作用,我无法切换抽屉。 Code 1 is not working but code 2 is working, can anyone help me figure out why ? 代码1无效,但是代码2有效,有人可以帮助我找出原因吗?

Code: 码:

    Burger menu icon code 1:

    // Inside Header
    navigationOptions: ({ navigation }) => ({
                // headerTitle: 'Rchampz',
                headerLeft:
                <TouchableOpacity  onPress={() => {navigation.dispatch(DrawerActions.toggleDrawer())} }>
                    <MenuImage navigation={navigation}/>
                </TouchableOpacity>,

//Created component and added inside navigationOptions

    const MenuImage = ({navigation}) => {
        if(!navigation.state.isDrawerOpen){
            return <TouchableOpacity><Image source={require('../assets/images/menu.png')} style={{width: 24, height: 24, resizeMode: 'contain' ,marginLeft: 15}}/></TouchableOpacity>
        }else{
            return <MenuIcon style={{paddingLeft: 10, paddingRight: 10}} name="md-arrow-back" size={30} color="black"/>
        }
    }

    Burger menu icon code 2:

    //Inside header

    navigationOptions: ({ navigation }) => ({
                // headerTitle: 'Rchampz',
                headerLeft:
                <TouchableOpacity  onPress={() => {navigation.dispatch(DrawerActions.toggleDrawer())} }>
                    <MenuImage navigation={navigation}/>
                </TouchableOpacity>,


//Created component and added inside navigationOptions

    const MenuImage = ({navigation}) => {
        if(!navigation.state.isDrawerOpen){
            return <MenuIcon style={{paddingLeft: 10, paddingRight: 10}} name="md-menu" size={30} color="black"/>
        }else{
            return <MenuIcon style={{paddingLeft: 10, paddingRight: 10}} name="md-arrow-back" size={30} color="black"/>
        }
    }

Because of the way that you have setup your menu image in the first one. 由于您在第一个菜单中设置菜单图像的方式。 You wrapped the Image within another TouchableOpacity which means that the inner TouchableOpacity is picking up the user touch event. 您将Image包装在另一个TouchableOpacity ,这意味着内部的TouchableOpacity正在拾取用户触摸事件。 This is not actually the behavior you want because you want the TouchableOpacity on headerLeft to be the receiver of the input. 因为你要的这实际上不是你想要的行为TouchableOpacity上headerLeft是输入的接收器。 So simply remove the "extra" TouchableOpacity . 因此,只需删除“额外的” TouchableOpacity

// Inside Header
navigationOptions: ({ navigation }) => ({
    headerLeft:
    <TouchableOpacity onPress={() => {navigation.dispatch(DrawerActions.toggleDrawer())} }>
        <MenuImage navigation={navigation}/>
    </TouchableOpacity>,

//Created component and added inside navigationOptions
const MenuImage = ({navigation}) => {
    if(!navigation.state.isDrawerOpen){
        // No need of Touchable here. Since you want the above Touchable to pick up the onPress event.
        return <Image source={require('../assets/images/menu.png')} style={{width: 24, height: 24, resizeMode: 'contain' ,marginLeft: 15}}/>. 
    }else{
        return <MenuIcon style={{paddingLeft: 10, paddingRight: 10}} name="md-arrow-back" size={30} color="black"/>
    }
}

The reason why the second example works is simply that, there is no extra Touchable handling the onPress event. 第二个示例起作用的原因很简单,没有额外的Touchable处理onPress事件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM