Skip to content
This repository was archived by the owner on Mar 17, 2025. It is now read-only.

Commit 7816708

Browse files
add listener types (#861)
* add-listener-types * Fix lint errors Stack.tsx * Update withLayoutContext.tsx to fix lint errors * Update useScreens.tsx to fix lint errors --------- Co-authored-by: Mark Lawlor <[email protected]>
1 parent 7b6032d commit 7816708

File tree

5 files changed

+39
-9
lines changed

5 files changed

+39
-9
lines changed
+5-1
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,19 @@
11
import {
22
createDrawerNavigator,
3+
DrawerNavigationEventMap,
34
DrawerNavigationOptions,
45
} from "@react-navigation/drawer";
6+
import { DrawerNavigationState, ParamListBase } from "@react-navigation/native";
57

68
import { withLayoutContext } from "./withLayoutContext";
79

810
const DrawerNavigator = createDrawerNavigator().Navigator;
911

1012
export const Drawer = withLayoutContext<
1113
DrawerNavigationOptions,
12-
typeof DrawerNavigator
14+
typeof DrawerNavigator,
15+
DrawerNavigationState<ParamListBase>,
16+
DrawerNavigationEventMap
1317
>(DrawerNavigator);
1418

1519
export default Drawer;

packages/expo-router/src/layouts/Stack.tsx

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
import { ParamListBase, StackNavigationState } from "@react-navigation/native";
12
import {
23
createNativeStackNavigator,
4+
NativeStackNavigationEventMap,
35
NativeStackNavigationOptions,
46
} from "@react-navigation/native-stack";
57

@@ -9,7 +11,9 @@ const NativeStackNavigator = createNativeStackNavigator().Navigator;
911

1012
export const Stack = withLayoutContext<
1113
NativeStackNavigationOptions,
12-
typeof NativeStackNavigator
14+
typeof NativeStackNavigator,
15+
StackNavigationState<ParamListBase>,
16+
NativeStackNavigationEventMap
1317
>(NativeStackNavigator);
1418

1519
export default Stack;

packages/expo-router/src/layouts/Tabs.tsx

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
import { Pressable } from "@bacons/react-views";
22
import {
33
BottomTabNavigationOptions,
4+
BottomTabNavigationEventMap,
45
createBottomTabNavigator,
56
} from "@react-navigation/bottom-tabs";
7+
import { TabNavigationState, ParamListBase } from "@react-navigation/native";
68
import React from "react";
79
import { Platform } from "react-native";
810

@@ -15,7 +17,9 @@ const BottomTabNavigator = createBottomTabNavigator().Navigator;
1517

1618
export const Tabs = withLayoutContext<
1719
BottomTabNavigationOptions & { href?: Href | null },
18-
typeof BottomTabNavigator
20+
typeof BottomTabNavigator,
21+
TabNavigationState<ParamListBase>,
22+
BottomTabNavigationEventMap
1923
>(BottomTabNavigator, (screens) => {
2024
// Support the `href` shortcut prop.
2125
return screens.map((screen) => {

packages/expo-router/src/layouts/withLayoutContext.tsx

+8-3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { EventMapBase, NavigationState } from "@react-navigation/native";
12
import React from "react";
23

34
import { useContextKey } from "../Route";
@@ -67,15 +68,19 @@ export function useFilterScreenChildren(
6768
/** Return a navigator that automatically injects matched routes and renders nothing when there are no children. Return type with children prop optional */
6869
export function withLayoutContext<
6970
TOptions extends object,
70-
T extends React.ComponentType<any>
71+
T extends React.ComponentType<any>,
72+
State extends NavigationState,
73+
EventMap extends EventMapBase
7174
>(
7275
Nav: T,
73-
processor?: (options: ScreenProps<TOptions>[]) => ScreenProps<TOptions>[]
76+
processor?: (
77+
options: ScreenProps<TOptions, State, EventMap>[]
78+
) => ScreenProps<TOptions, State, EventMap>[]
7479
): React.ForwardRefExoticComponent<
7580
React.PropsWithoutRef<PickPartial<React.ComponentProps<T>, "children">> &
7681
React.RefAttributes<unknown>
7782
> & {
78-
Screen: (props: ScreenProps<TOptions>) => null;
83+
Screen: (props: ScreenProps<TOptions, State, EventMap>) => null;
7984
} {
8085
const Navigator = React.forwardRef(
8186
(

packages/expo-router/src/useScreens.tsx

+16-3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
import type {
2+
EventMapBase,
3+
NavigationState,
4+
ParamListBase,
5+
RouteProp,
6+
ScreenListeners,
7+
} from "@react-navigation/native";
18
import React from "react";
29

310
import {
@@ -15,7 +22,9 @@ import { SuspenseFallback } from "./views/SuspenseFallback";
1522
import { Try } from "./views/Try";
1623

1724
export type ScreenProps<
18-
TOptions extends Record<string, any> = Record<string, any>
25+
TOptions extends Record<string, any> = Record<string, any>,
26+
State extends NavigationState = NavigationState,
27+
EventMap extends EventMapBase = EventMapBase
1928
> = {
2029
/** Name is required when used inside a Layout component. */
2130
name?: string;
@@ -27,8 +36,12 @@ export type ScreenProps<
2736
initialParams?: { [key: string]: any };
2837
options?: TOptions;
2938

30-
// TODO: types
31-
listeners?: any;
39+
listeners?:
40+
| ScreenListeners<State, EventMap>
41+
| ((prop: {
42+
route: RouteProp<ParamListBase, string>;
43+
navigation: any;
44+
}) => ScreenListeners<State, EventMap>);
3245

3346
getId?: ({
3447
params,

0 commit comments

Comments
 (0)