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:
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