Skip to content

Commit

Permalink
feat: improve useNavigationState typing (#9464)
Browse files Browse the repository at this point in the history
  • Loading branch information
jemmyphan authored Mar 29, 2021
1 parent 5473982 commit 84020a0
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions packages/core/src/useNavigationState.tsx
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
import * as React from 'react';
import type { NavigationState } from '@react-navigation/routers';
import type { NavigationState, ParamListBase } from '@react-navigation/routers';
import useNavigation from './useNavigation';
import type { NavigationProp } from './types';

type Selector<T> = (state: NavigationState) => T;
type Selector<ParamList extends ParamListBase, T> = (
state: NavigationState<ParamList>
) => T;

/**
* Hook to get a value from the current navigation state using a selector.
*
* @param selector Selector function to get a value from the state.
*/
export default function useNavigationState<T>(selector: Selector<T>): T {
const navigation = useNavigation();
export default function useNavigationState<ParamList extends ParamListBase, T>(
selector: Selector<ParamList, T>
): T {
const navigation = useNavigation<NavigationProp<ParamList>>();

// We don't care about the state value, we run the selector again at the end
// The state is only to make sure that there's a re-render when we have a new value
Expand Down

0 comments on commit 84020a0

Please sign in to comment.