【问题标题】:Custom navigation operation on specific tab button of react-navigation's tab navigatorreact-navigation 标签导航器特定标签按钮的自定义导航操作
【发布时间】:2019-07-30 19:06:36
【问题描述】:

我想从选项卡上的特定按钮打开抽屉(例如选项菜单),而不是导航到屏幕。我目前的解决方案是在 react-navigation v2 上工作,但是当我们从 v2 升级到 react-navigation 的 v3 和从 v57 升级到 react-native 的 v60 时,该解决方案已经停止工作。

标签栏中的菜单标签按钮分配了一个虚拟屏幕,我正在使用tabBarOnPress()拦截导航操作。该方法打开抽屉,如果它与菜单按钮的路由名称匹配,则返回,否则它会导航。似乎标签导航器正在导航到虚拟屏幕,无论我分配给tabBarOnPress() 的任何方法,并且该方法也被调用。

以下是在 v2 中运行良好但在 v3 中停止运行的当前代码:

class SlideMenuScreen extends Component {

    render() {
        return null;
    }
}


const tab = createBottomTabNavigator({
    Products: {
        screen: AppStack,
        navigationOptions: {
            tabBarLabel: 'Home',
            tabBarIcon: ({ tintColor }) => (
                <SimpleLineIcons name='home' size={20} color={tintColor} />
            )
        }
    },
    Cart: {
        screen: CartScreen,
        navigationOptions: {
            tabBarLabel: 'Cart',
            tabBarIcon: ({ tintColor }) => (
                <EvilIcons
                    reverse
                    name='cart'
                    type='font-awesome'
                    color={tintColor}
                    size={30}
                />
            )
        }
    },
    SignIn: {
        screen: AuthStack,
        navigationOptions: {
            tabBarLabel: 'Sign in',
            tabBarIcon: ({ tintColor }) => (
                <SimpleLineIcons
                    name='login'
                    color={tintColor}
                    size={20}
                />
            )
        }
    },
    SideMenu: {
        screen: SlideMenuScreen,
        navigationOptions: (props) => ({
            tabBarLabel: 'Menu',
            tabBarIcon:
                <Entypo
                    name='menu'
                    color={props.tintColor}
                    size={20}
                />
        })
    }
},
    {
        initialRouteName: 'Products',
        swipeEnabled: true,
        tabBarOptions: {
            showLabel: false,
            showIcon: true,
            activeTintColor: config.themeBackgroundColor,
            inactiveTintColor: 'grey',
        },
    }
);


tab.navigationOptions = ({ navigation }) => {

    const { routeName } = navigation.state.routes[navigation.state.index];
    if (routeName === 'SideMenu') {
        navigation.openDrawer();
        return;
    }
    navigation.navigate(routeName);
};


const sideMenu = createDrawerNavigator({
    Home: tab
}, {
        initialRouteName: 'Home',
        drawerPosition: 'right',
        drawerOpenRoute: 'DrawerOpen',
        drawerCloseRoute: 'DrawerClose',
        drawerToggleRoute: 'DrawerToggle',
        drawerWidth: 250,
        contentComponent: signedOutDrawerContent
    }
);

【问题讨论】:

    标签: react-native react-navigation react-navigation-drawer react-native-tabnavigator


    【解决方案1】:

    您可以通过在navigationOptions 上使用tabBarOnPress 将默认选项卡图标按下处理程序更改为您想要的任何内容:

     Search: {
      screen: SearchStack,
      navigationOptions: {
        tabBarIcon: ({ tintColor }) => <Icon name='search' color={tintColor} size={25} />,
        tabBarOnPress: () => alert('hello')
      }
    },
    

    【讨论】:

    • 虽然解决了问题,但我有点困惑。正如文档所述,navigationOptions 已重命名为defaultNavigationOptions,但它仍然接受navigationOptions 用于tabNavigator 中的选项卡,但是,如果将defaultNavigationOptions 提供给tabNavigator 本身,它会正确接受defaultNavigationOptions。然而,这种行为在 v3 中很明显。
    • 我在文档中没有看到这样的东西。 navigationOptions : Navigation options for the navigator itself, to configure a parent navigator。 defaultNavigationOptions :Default navigation options to use for screens。 2上面的句子是文档中写的。 navigationOptions 用于选项卡按钮 itelf 但defaultNavigationOptions 用于选项卡堆栈中的每个屏幕
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-01-30
    • 2020-06-12
    • 2020-09-16
    • 2014-06-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多