【问题标题】:Resetting screens to default react-native navigation将屏幕重置为默认的 react-native 导航
【发布时间】:2019-08-31 23:23:31
【问题描述】:

我正在使用这个带有底部标签的库: 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,
    }

【问题讨论】:

    标签: react-native react-native-navigation wix-react-native-navigation


    【解决方案1】:

    首先,如果用户单击底部选项卡,您必须添加侦听器。在registerbottomtabselectedlistener 的帮助下,您可以实现这一目标。您可以使用popToRoot 将用户发送到堆栈根目录

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

    【讨论】:

      【解决方案2】:

      从您发布的 sn-p 中不确定,但我认为您正试图让底部标签正常工作。这是不完整的,但希望这能让你走上正轨。

      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',
      }));
      

      【讨论】: