将屏幕重置为默认的 react-native 导航

Resetting screens to default react-native navigation

我正在使用这个带有底部标签的库: https://github.com/wix/react-native-navigation

对于导航,我在底部有 3 个选项卡,其中一个用于主页,问题是我从主屏幕移动到另一个屏幕,它被添加到堆栈中,我希望能够重置堆栈每当我再次点击底部选项卡的主页图标时。

底部选项卡的主页图标route.js是这样的:

stack: {
  children: [
    {
      component: {
        name: home,
      }
    },
  ],
  options: {
    bottomTab: {
      iconInsets: {top: 6, left: 0, bottom: -6, right: 0},
      titleDisplayMode: 'alwaysHide',
      icon: require('../assets/images/home.png'),
      selectedIconColor: colors.primary,
    }

从您发布的代码片段中不确定,但我认为您正在尝试让底部标签起作用。这是不完整的,但希望这能让你走上正轨。

const AppTabs = createBottomTabNavigator({
  App: AppStack, // stack navigator
  Contacts: ContactsStack, // stack navigator
  Settings: {
    screen: SettingsScreen, // single component import
    navigationOptions: {
      tabBarLabel: 'Settings',
      tabBarIcon: ({ focused }) => (
        <Ionicons
          size={26}
          name={Platform.OS === 'ios' ? 'ios-settings' : 'settings'}
          style={{ marginBottom: -3 }}
          color={focused ? "red" : "gray"}
        />
      ),
    }
  },
}, {
  initialRouteName: 'App',
  tabBarPosition: 'bottom',
  swipeEnabled: true,
  tabBarOptions: {
    activeTintColor: 'green',
    inactiveTintColor: 'gray',
    labelStyle: {
      fontSize: 16,
    },
    tabStyle: { marginBottom: -10 }
  }
});

export default createAppContainer(createSwitchNavigator({
  AuthLoading: AuthLoadingScreen,
  App: AppTabs,
  Auth: AuthStack,
}, {
  initialRouteName: 'AuthLoading',
}));

首先,如果用户单击底部选项卡,您必须添加侦听器。在 registerbottomtabselectedlistener you can achieve this . You can use popToRoot 的帮助下将用户发送到堆栈的根目录

// Subscribe
const bottomTabEventListener = Navigation.events().registerBottomTabSelectedListener(({ selectedTabIndex, unselectedTabIndex }) => {
   Navigation.popTo(componentId);  // check selectedTabIndex and send to home
});
...
// Unsubscribe
bottomTabEventListener.remove();