Skip to content
This repository was archived by the owner on Dec 3, 2022. It is now read-only.

fix useNavigation generic #69

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions src/Hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,21 @@ import {
EventType,
} from 'react-navigation';

export function useNavigation<S>(): NavigationScreenProp<S & NavigationRoute> {
const navigation = useContext(NavigationContext) as any; // TODO typing?
type NavigationType<S> = S & NavigationScreenProp<NavigationRoute>;

type NavigationWithParamType<S, P> = NavigationType<S> & NavigationScreenProp<NavigationRoute, P>;

export function useNavigation<S>(): NavigationType<S>;

export function useNavigation<S, P>(): NavigationWithParamType<S, P>;

export function useNavigation() {
const navigation = useContext(NavigationContext);
if (!navigation) {
throw new Error(
"react-navigation hooks require a navigation context but it couldn't be found. " +
"Make sure you didn't forget to create and render the react-navigation app container. " +
'If you need to access an optional navigation object, you can useContext(NavigationContext), which may return'
"Make sure you didn't forget to create and render the react-navigation app container. " +
'If you need to access an optional navigation object, you can useContext(NavigationContext), which may return'
);
}
return navigation;
Expand Down