Files
react-navigation/packages/core/src/useNavigation.tsx
2019-08-01 23:40:57 +01:00

23 lines
601 B
TypeScript

import * as React from 'react';
import NavigationContext from './NavigationContext';
import { NavigationProp, ParamListBase } from './types';
/**
* Hook to access the navigation prop of the parent screen anywhere.
*
* @returns Navigation prop of the parent screen.
*/
export default function useNavigation<
T extends NavigationProp<ParamListBase>
>(): T {
const navigation = React.useContext(NavigationContext);
if (navigation === undefined) {
throw new Error(
"We couldn't find a navigation object. Is your component inside a navigator?"
);
}
return navigation as T;
}