How to reset the state of a StackNavigator nested in a DrawerNavigatior?

I am building an app whose navigation is based on a DrawerNavigator from the react-navigation library.

This navigator has 3 tabs:

  • 2 regular tabs
  • 1 StackNavigator named "Search"
  • The StackNavigator consists of one screen that lets the user search for an item, and a second screen where the user sees the search results.

    I do not want the search results page to be a tab of a DrawerNavigator, this is why I implemented this structure.

    The problem is: if the user has already performed a search, when he clicks on the "Search" tab, he does not come back to the search screen but to the search results screen. I would prefer that the user comes back to the search screen.

    How can I achieve that?


    您可以通过navigationActions使用导航调度来实现此目的

    import { NavigationActions } from 'react-navigation';
    
    const resetAction = NavigationActions.reset({
      index: 0,
      actions: [
        NavigationActions.navigate({
          routeName: 'DrawerScreen',
          params: {},
          action: NavigationActions.navigate({ routeName: 'SearchScreen' }),
        }),
      ],
    })
    navigation.dispatch(resetAction)
    

    import { NavigationActions } from 'react-navigation';
    
    const resetAction = NavigationActions.reset({
      index: 0,
      actions: [
          NavigationActions.navigate({ routeName: 'SearchScreen'})
      ]
    })
    

    在你的按钮或任何带有事件的元素中添加:this.props.navigation.dispatch(resetAction)

    <Button
        onPress= {
            () => this.props.navigation.dispatch(resetAction)
        }
        title='Back to Search'
    />
    
    链接地址: http://www.djcxy.com/p/39830.html

    上一篇: `如果constexpr`,在lambda里面,包内部扩展

    下一篇: 如何重置嵌套在DrawerNavigatior中的StackNavigator的状态?