Skip to content

Instantly share code, notes, and snippets.

@dac09
Last active January 2, 2018 17:29
Show Gist options
  • Save dac09/f05a3b6f6faeb96ebaf3d1b1cdf4063b to your computer and use it in GitHub Desktop.
Save dac09/f05a3b6f6faeb96ebaf3d1b1cdf4063b to your computer and use it in GitHub Desktop.
HOC With TabNavigator React Navigation
type Props = {
navigation: NavigationScreenProp<*>
};
const TabNavigatorInstance = TabNavigator(tabRouteConfig, tabNavigationConfig);
class TabbedNavigator extends Component<Props> {
// This is required for it to work, somewhat undocumented
// https://github.com/react-navigation/react-navigation/issues/3076
static router = TabNavigatorInstance.router;
componentDidMount() {
BackHandler.addEventListener('hardwareBackPress', this.handleBack);
}
componentWillUnmount() {
BackHandler.removeEventListener('hardwareBackPress', this.handleBack);
}
handleBack = () => {
const { navigation } = this.props;
if (navigation.index === 0) {
return false;
}
navigation.dispatch(NavigationActions.back());
return true;
};
render() {
return <TabNavigatorInstance {...this.props} />;
}
}
export default TabbedNavigator;
@dac09
Copy link
Author

dac09 commented Jan 2, 2018

Adds life cycle hooks to Tab Navigator using a higher order component.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment