Skip to content

Instantly share code, notes, and snippets.

@HristoEftimov
Last active February 25, 2018 21:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save HristoEftimov/542c1e0c6275bfee4ea4fed8a8ab4db4 to your computer and use it in GitHub Desktop.
Save HristoEftimov/542c1e0c6275bfee4ea4fed8a8ab4db4 to your computer and use it in GitHub Desktop.
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