【问题标题】:How to make "Go back to initial StackNavigator Screen when TabNavigator's Tab is pressed"如何制作“按下 Tab Navigator Tab 时返回初始 Stack Navigator 屏幕”
【发布时间】:2018-05-21 04:32:30
【问题描述】:

简介

就像 Facebook、Instagram 和任何其他移动应用程序一样,我想实现“返回 Stacknavigator 中的初始屏幕”

  1. 如果用户按下按钮,
  2. 它返回到第一页。

简单用例

  1. 参见 TabNavigator
  2. 转到“Feed”标签
  3. 转到“用户”屏幕
  4. 转到另一个“用户”屏幕
  5. 按主选项卡图标 - 'Feed'}
  6. 返回到“Feed”选项卡 // 这样您就不会看到“返回”按钮

如果你不理解这个用例,请留下评论,我会为你画出它的状态流

我的 Main TabNavigator 上的图标代码。

navigationOptions: ({ navigation }) => ({
      header: null,
      tabBarIcon: ({ focused }) => {
          const { routeName } = navigation.state;
          ....
          return (
              <TochableWithoutFeedback onPress={()=>{navigation.goback(iconName)}> 
              // undefined is not a function when I press the Button 
              // deeper screen. (not getting any error on Main)
                  <Ionicons
                      name={iconName}
                      size={30}
                      style={{ marginBottom: -3 }}
                      color={focused ? Colors.tabIconSelected : Colors.tabIconDefault}
                      />
             <TochableWithoutFeedback>
         );
  },

【问题讨论】:

标签: react-native react-navigation


【解决方案1】:

实际上,这取决于您的路由有多深,例如 Instagram 2 到 3 深路由,否则是标签

这样您就可以重置您的路由器或使用

返回主路由器
this.props.navigation.goBack(null)

例如。

Tab navigation child ahs Stack navigation 所以在 Stack 中,你可以做类似的事情

// Anyone watching this, please check your react-navigation version
// I'm using ^1.0.0-beta.21.
// props for tabBarOnpress varies can be varied (Editor John Baek)
tabBarOnPress: ({scene, jumpToIndex}) => {
      jumpToIndex(scene.index)
      navigation.dispatch(NavigationActions.reset({
        index: 0,
        actions: [
          NavigationActions.navigate({ routeName: 'home' }) // go to first screen of the StackNavigator
        ]
      }))
    }

所以每当有人按 Home Tab 时,所有路线都会重置,您会看到像 Instagram 一样的 Feed 屏幕

TabNavigation 
  -Home
      |
      StakeNavigation 
                    |
                    mainScreen //main of home 
                    Subrouts 
                    RouteToProfile //just like insta

  -Search 
        |
         StakeNavigation 
                       |
                       mainScreen //main of search 
                       searchResult //if people serch some specific 

然后继续……所以在 Tab 的 stakeNavigation 级别重置路线

const SubHome = StackNavigator({
  Home: { screen: Home },
  Home2: { screen: Home2 },
  Home3 : { screen: Home3 },
},{
  navigationOptions:({navigation,screenProps})=>({
    tabBarLabel: I18n.t('tab_car'),
    tabBarIcon: ({ tintColor }) => (
      <CustomIcon name={'Home'} width={28} height={30} fill={tintColor} />
    ),
    tabBarOnPress: (tab, jumpToIndex) => {
      jumpToIndex(tab.index)
      navigation.dispatch(NavigationActions.reset({
        index: 0,
        actions: [
          NavigationActions.navigate({ routeName: 'Home' }) // go to first screen of the StackNavigator
        ]
      }))
    }
  }),
  headerMode:'none'
});

【讨论】:

  • 这篇文章解释了如何从navigationOptions 调用navigation.dispatchstackoverflow.com/a/45098153/681830
  • 我的代码真的适用于我的应用程序不要只是所有内容,例如仅使用 TabBarPress
  • avigationBar 由于 headerMode:'none' 而消失
  • umm.. 还有一个转折,我在我的应用程序中遇到了这个问题,所以我在这里找到了解决方案 "react-navigation": "github.com/react-community/…",
  • 我正在使用特定版本的导航进行标签点击
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-03-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多