Skip to content

Instantly share code, notes, and snippets.

@ameernormie
Created December 23, 2019 07:52
Show Gist options
  • Save ameernormie/8a41c86950d907807b5c7678a274a913 to your computer and use it in GitHub Desktop.
Save ameernormie/8a41c86950d907807b5c7678a274a913 to your computer and use it in GitHub Desktop.
/**
* This service will be used if we don't have access to navigation
* prop in the component. Usually the components that are outside the
* heirarchy of the nav screens don't have access to the navigation params.
*/
import { NavigationActions } from 'react-navigation';
import { defaultTo } from 'ramda';
let _navigator: any;
function setTopLevelNavigator(navigatorRef: any) {
_navigator = navigatorRef;
}
function navigate(routeName: string, params?: object) {
try {
_navigator.dispatch(
NavigationActions.navigate({
routeName,
params: defaultTo({}, params),
}),
);
} catch (error) {
// console.log('error in navigation', error)
}
}
function goBackToPreviousScreen() {
try {
_navigator.dispatch(NavigationActions.back());
} catch (error) {
// console.log('error in navigation', error)
}
}
// add other navigation functions that you need and export them
export default {
navigate,
goBackToPreviousScreen,
setTopLevelNavigator,
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment