From 35233e948303caceb0b85705b0b80b215b58e803 Mon Sep 17 00:00:00 2001 From: A389216 Date: Mon, 18 Nov 2019 15:44:41 +0100 Subject: [PATCH] Fix useNavigation params typing This fix react-navigation/hooks#49 --- src/Hooks.ts | 7 +++++-- src/__tests__/Hooks.test.tsx | 12 +++++++++--- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/src/Hooks.ts b/src/Hooks.ts index 0cdae3b..1b9d761 100644 --- a/src/Hooks.ts +++ b/src/Hooks.ts @@ -17,7 +17,10 @@ import { EventType, } from 'react-navigation'; -export function useNavigation(): NavigationScreenProp { +export function useNavigation(): NavigationScreenProp< + S & NavigationRoute, + P +> { const navigation = useContext(NavigationContext) as any; // TODO typing? if (!navigation) { throw new Error( @@ -32,7 +35,7 @@ export function useNavigation(): NavigationScreenProp { export function useNavigationParam( paramName: T ) { - return useNavigation().getParam(paramName); + return useNavigation().getParam(paramName); // TODO typing ? } export function useNavigationState() { diff --git a/src/__tests__/Hooks.test.tsx b/src/__tests__/Hooks.test.tsx index e40631d..ea34601 100644 --- a/src/__tests__/Hooks.test.tsx +++ b/src/__tests__/Hooks.test.tsx @@ -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

{from}

; + const id = useNavigation().getParam('id'); + return ( +

+ {from}:{id} +

+ ); }; const OtherScreen = () => { @@ -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(() => {