React Navigation:一个带有堆栈的抽屉,您想在某些屏幕上隐藏该抽屉,但它不会隐藏

时间:2018-08-14 15:54:06

标签: react-native react-navigation drawer

我正面临intervals所述的情况,我有一个带堆栈的抽屉,我想在某些屏幕上隐藏抽屉。不幸的是,以下代码受文档的影响而无法正常工作,并且抽屉仍然可以在推送的堆栈屏幕上打开。

const MenuStack = createStackNavigator(
  {
    CheckedInMenu: { screen: MenuScreen },
    CheckedIdMenuItemDetail: { screen: MenuItemDetailScreen }
  },
  {
    navigationOptions: ({ navigation }) => {
      let options = {
        headerTitleStyle: {
          color: headerColor
        },
        headerBackTitleStyle: {
          color: headerColor
        },
        headerTintColor: headerColor
      };
      let drawerLockMode = "unlocked";
      if (navigation.state.index > 0) {
        drawerLockMode = "locked-closed";
      }
      return { ...options, drawerLockMode };
    }
  }
);

const checkedInDrawer = createDrawerNavigator(
  {
    MenuStack: {
      screen: MenuStack,
      navigationOptions: {
        drawerLabel: SCREEN_TEXT_MENU_HEADER,
        drawerIcon: ({ tintColor }) => (
          <Image
            source={require("../assets/icons/menu.png")}
            resizeMode="contain"
            style={{ width: 25, height: 25, tintColor: tintColor }}
          />
        )
      }
    }
  },
  {
    initialRouteName: "MenuStack",
    drawerBackgroundColor: backgroundColor,
    contentComponent: BurgerMenu,
    contentOptions: {
      activeTintColor: activeTintColor,
      inactiveTintColor: headerColor,
      activeBackgroundColor: backgroundColor,
      itemStyle: { borderBottomWidth: 1, borderColor: borderColor },
      labelStyle: { fontSize: 16, fontWeight: "500" }
    }
  }
);

我在做什么错了?

编辑

即使我console.log()像这样的一切:

  let options = {
    headerTitleStyle: {
      color: headerColor
    },
    headerBackTitleStyle: {
      color: headerColor
    },
    headerTintColor: headerColor
  };
  let drawerLockMode = "unlocked";
  console.log(navigation);
  if (navigation.state.routeName !== "CheckedInMenu") {
    drawerLockMode = "locked-closed";
  }
  if (navigation.state) console.log(navigation.state.routeName);
  console.log({ ...options, drawerLockMode: drawerLockMode });
  return { ...options, drawerLockMode: drawerLockMode };

在CheckedInMenuItemDetailScreen上说drawerLockMode = "locked-closed"

编辑2: 我现在发现,实现这一目标的唯一方法就是文档所说的方法。您必须这样做:

MainStack.navigationOptions = ({ navigation }) => {
  let drawerLockMode = "unlocked";
  if (navigation.state.index > 0) {
    drawerLockMode = "locked-closed";
  }
  return {
    drawerLockMode
  };
};

并且您必须尝试在堆栈的定义的navigationOptions中进行操作,就像我在上面的原始帖子中所做的那样。记住这一点!

1 个答案:

答案 0 :(得分:1)

此代码有效。导航到DetailsS​​creen时,DrawerMenu隐藏。我已经使用您的referenced the offical docs here实现了它。

import React, { Component } from 'react';
import { Platform, StyleSheet, TouchableHighlight, Text, View } from 'react-native';

import { createStackNavigator, createDrawerNavigator, createSwitchNavigator } from 'react-navigation';




class ProfileScreen extends Component {
    render() {
        return (
            <View>
                <Text> ProfileScreen </Text>
            </View>
        )
    }
}

class DetailsScreen extends Component {
    render() {
        return (
            <View>
                <Text> DetailsScreen </Text>
            </View>
        )
    }
}


class HomeScreen extends Component {
    render() {
        const { navigate } = this.props.navigation
        return (
            <View>
                <Text> HomeScreen </Text>
                <TouchableHighlight
                    onPress={() => navigate("Details", { screen: "DetailsScreen" })}
                >
                    <Text>Screen One </Text>
                </TouchableHighlight>
            </View>
        )
    }
}


const FeedStack = createStackNavigator({
    FeedHome: {
        screen: HomeScreen,
        navigationOptions: {
            title: "test"
        }
    },
    Details: DetailsScreen,
});

FeedStack.navigationOptions = ({ navigation }) => {
    let drawerLockMode = 'unlocked';
    if (navigation.state.index > 0) {
        drawerLockMode = 'locked-closed';
    }

    return {
        drawerLockMode,
    };
};

const DrawerNavigator = createDrawerNavigator({
    Home: FeedStack,
    Profile: ProfileScreen,
});

const AppNavigator = createSwitchNavigator(
    {
        Drawer: DrawerNavigator,
    }
);

export default class App extends Component {
    render() {
        return (
            <View style={{ flex: 1 }} >
                <AppNavigator />
            </View>
        );
    }
}