If you don´t want to use custom back buttons (e. g. to avoid dealing with the different "native" button designs), there is also the possibility to reset the navigation state. In your case like this:
import { NavigationActions } from 'react-navigation';
const resetAction = NavigationActions.reset({
index: 2,
actions: [
NavigationActions.navigate({ routeName: 'Home' }),
NavigationActions.navigate({ routeName: 'BookTicket' }),
NavigationActions.navigate({ routeName: 'MyBookings' }),
],
});
this.props.navigation.dispatch(resetAction);
This way you just set a stack of navigations and let it jump to the index position.
This setup must be done at the departing scene, so that in the next scene the back button works as expected.
But I prefer this way anyway, because it allows to maintain the default look of the header and keeps the stack clean. If you use navigation.navigate
instead of navigation.back
the stack becomes totally wrong for the back button.
navigation.navigate([path])
? – Pentosan