Last active
February 25, 2018 21:40
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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