是的,完全可以使用 react-navigation 重置堆栈,对此有特定的操作。
这是抽屉位于选项卡导航中的示例。
首先我们在 中定义我们的抽屉MenuDrawer.js
,这里没什么特别的:
import { createDrawerNavigator } from 'react-navigation';
import ProductStack from './ProductStack';
import OtherStack from './OtherStack';
const MenuDrawer = createDrawerNavigator({
Product: {
screen: ProductStack,
navigationOptions: {
drawerLabel: 'My products',
},
},
Other: {
screen: OtherStack,
navigationOptions: {
drawerLabel: 'Something else',
},
},
});
export default MenuDrawer;
最后我们定义了我们的标签导航,我们使用tabBarOnPress
导航选项来监听任何抽屉打开并在需要时重置堆栈:
import { createBottomTabNavigator, StackActions, NavigationActions } from 'react-navigation';
import MenuDrawer from './MenuDrawer';
const BottomTabs = createBottomTabNavigator({
Menu: {
screen: MenuDrawer,
navigationOptions: {
title: 'Menu',
tabBarOnPress: ({ navigation }) => {
const notResetRoute = navigation.state.routes.find(x => x.index > 0); // Check for stack not positioned at the first screen
if (notResetRoute) {
const resetAction = StackActions.reset({ // We reset the stack (cf. documentation)
index: 0,
actions: [
NavigationActions.navigate({ routeName: notResetRoute.routes[0].routeName }),
],
});
navigation.dispatch(resetAction);
}
navigation.openDrawer(); // Finally we open the drawer
},
},
},
});
export default BottomTabs;
当用户点击相应的选项卡时,抽屉就会打开。显然,如果抽屉以不同的方式打开,例如通过单击给定屏幕中的按钮,则可以进行相同的操作。重要的是同时重置堆栈。