Skip to content

Commit

Permalink
Fix useNavigation params typing
Browse files Browse the repository at this point in the history
  • Loading branch information
a389216 committed Nov 18, 2019
1 parent 0b08542 commit 35233e9
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
7 changes: 5 additions & 2 deletions src/Hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@ import {
EventType,
} from 'react-navigation';

export function useNavigation<S>(): NavigationScreenProp<S & NavigationRoute> {
export function useNavigation<S, P = NavigationParams>(): NavigationScreenProp<
S & NavigationRoute,
P
> {
const navigation = useContext(NavigationContext) as any; // TODO typing?
if (!navigation) {
throw new Error(
Expand All @@ -32,7 +35,7 @@ export function useNavigation<S>(): NavigationScreenProp<S & NavigationRoute> {
export function useNavigationParam<T extends keyof NavigationParams>(
paramName: T
) {
return useNavigation().getParam(paramName);
return useNavigation().getParam(paramName); // TODO typing ?
}

export function useNavigationState() {
Expand Down
12 changes: 9 additions & 3 deletions src/__tests__/Hooks.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,23 @@ import {

interface DetailsScreenParams {
from: string;
id: number;
}

const HomeScreen = () => {
const { navigate } = useNavigation();
const params: DetailsScreenParams = { from: 'Home' };
const params: DetailsScreenParams = { from: 'Home', id: 1 };
return navigate('Details', params);
};

const DetailsScreen = () => {
const from = useNavigationParam('from');
return <p>{from}</p>;
const id = useNavigation<unknown, DetailsScreenParams>().getParam('id');
return (
<p>
{from}:{id}
</p>
);
};

const OtherScreen = () => {
Expand Down Expand Up @@ -102,7 +108,7 @@ describe('AppNavigator1 Stack', () => {

it('useNavigationParam: Get passed parameter', () => {
const children = navigationContainer.toJSON().children;
expect(children).toContain('Home');
expect(children).toStrictEqual(['Home', ':', '1']);
});

afterEach(() => {
Expand Down

0 comments on commit 35233e9

Please sign in to comment.