I am stuck with an error. When I am trying to reset the screen navigation stack, I am getting this error The action 'Navigation/RESET' was not handled by any navigation
My code to reset the navigation is :
let resetAction = StackActions.reset({
key: null,
index: 0,
actions: [
NavigationActions.navigate({ routeName: 'ScreenName' })
]
})
this.props.navigation.dispatch(resetAction)
Please if you can suggest any solution, that would be very much appreciated.
In version 5.x, the way you can reset the navigation is changed. you can use :
import { StackActions } from '@react-navigation/native';
this.props.navigation.dispatch(
StackActions.popToTop()
);
To pop all page redirects from the stack and then go back to first screen. (view it on expo)
Or you can use:
import { CommonActions } from '@react-navigation/native';
navigation.dispatch(
CommonActions.reset({
index: 1,
routes: [
{ name: 'Home' },
{
name: 'Profile',
params: { user: 'jane' },
},
...
],
})
);
To reset your navigation state to an initial state.
Here is a live demo on snack expo (use android tab, go from home to details
page, then page 3
, then press reset. you can see it's now on home page and if you press back, it will exit.) (the code for popToTop
is on line 44)
Docs: pop to top action reset action
PS: If you're not using version 5.x , first make sure you have a route called ScreenName
(since you're using : routeName: 'ScreenName'
, and if it didn't fix it, try other codes to reset, like: here )