11import { useState , useContext , useEffect } from 'react' ;
22import { NavigationContext } from '@react-navigation/core' ;
3+ // TODO: Remove "react-navigation-types-only" when https://github.com/react-navigation/react-navigation/pull/5276
4+ // get merged
5+ import {
6+ NavigationScreenProp ,
7+ NavigationRoute ,
8+ NavigationParams ,
9+ NavigationEventCallback ,
10+ NavigationEventPayload ,
11+ EventType ,
12+ } from 'react-navigation-types-only' ;
313
4- export function useNavigation ( ) {
5- return useContext ( NavigationContext ) ;
14+ export function useNavigation < S > ( ) : NavigationScreenProp < S & NavigationRoute > {
15+ return useContext ( NavigationContext as any ) ;
616}
717
8- export function useNavigationParam ( paramName ) {
18+ export function useNavigationParam < T extends keyof NavigationParams > (
19+ paramName : T ,
20+ ) {
921 return useNavigation ( ) . getParam ( paramName ) ;
1022}
1123
@@ -17,7 +29,7 @@ export function useNavigationKey() {
1729 return useNavigation ( ) . state . key ;
1830}
1931
20- export function useNavigationEvents ( handleEvt ) {
32+ export function useNavigationEvents ( handleEvt : NavigationEventCallback ) {
2133 const navigation = useNavigation ( ) ;
2234 useEffect (
2335 ( ) => {
@@ -53,9 +65,8 @@ const didFocusState = { ...emptyFocusState, isFocused: true };
5365const willBlurState = { ...emptyFocusState , isBlurring : true } ;
5466const didBlurState = { ...emptyFocusState , isBlurred : true } ;
5567const willFocusState = { ...emptyFocusState , isFocusing : true } ;
56- const getInitialFocusState = isFocused =>
57- isFocused ? didFocusState : didBlurState ;
58- function focusStateOfEvent ( eventName ) {
68+ const getInitialFocusState = ( isFocused : boolean ) => isFocused ? didFocusState : didBlurState ;
69+ function focusStateOfEvent ( eventName : EventType ) {
5970 switch ( eventName ) {
6071 case 'didFocus' :
6172 return didFocusState ;
@@ -74,7 +85,7 @@ export function useFocusState() {
7485 const navigation = useNavigation ( ) ;
7586 const isFocused = navigation . isFocused ( ) ;
7687 const [ focusState , setFocusState ] = useState ( getInitialFocusState ( isFocused ) ) ;
77- function handleEvt ( e ) {
88+ function handleEvt ( e : NavigationEventPayload ) {
7889 const newState = focusStateOfEvent ( e . type ) ;
7990 newState && setFocusState ( newState ) ;
8091 }
0 commit comments