UNPKG

1.09 kBTypeScriptView Raw
1import type { Route } from '@react-navigation/routers';
2
3import { CHILD_STATE } from './useRouteCache';
4
5export default function getFocusedRouteNameFromRoute(
6 route: Partial<Route<string>>
7): string | undefined {
8 // @ts-expect-error: this isn't in type definitions coz we want this private
9 const state = route[CHILD_STATE] ?? route.state;
10 const params = route.params as { screen?: unknown } | undefined;
11
12 const routeName = state
13 ? // Get the currently active route name in the nested navigator
14 state.routes[
15 // If we have a partial state without index, for tab/drawer, first screen will be focused one, and last for stack
16 // The type property will only exist for rehydrated state and not for state from deep link
17 state.index ??
18 (typeof state.type === 'string' && state.type !== 'stack'
19 ? 0
20 : state.routes.length - 1)
21 ].name
22 : // If state doesn't exist, we need to default to `screen` param if available
23 typeof params?.screen === 'string'
24 ? params.screen
25 : undefined;
26
27 return routeName;
28}