Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
React Native and ReactNavigation: Disable Android's hardware back button for specific screen
const defaultGetStateForAction = AppNavigator.router.getStateForAction;
AppNavigator.router.getStateForAction = (action, state) => {
const screen = state ? state.routes[state.index] : null;
const tab = screen && screen.routes ? screen.routes[screen.index] : null;
const tabScreen = tab && tab.routes ? tab.routes[tab.index] : null;
if (
action.type === NavigationActions.BACK &&
tab && tab.routeName === 'EventsTab' &&
tabScreen && tabScreen.routeName === 'events'
) {
// Option 1: will close the application
// return null;
// Option 2: will keep the app open
const newRoutes = state.routes.filter(r => r.routeName !== 'auth');
const newIndex = newRoutes.length - 1;
return defaultGetStateForAction(action, {
index: newIndex,
routes: newRoutes
});
}
return defaultGetStateForAction(action, state);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.