Skip to content

Commit 83a2359

Browse files
author
Artur Burlak
committed
Fix TS issues for Screens and withProvider
1 parent 94da3e4 commit 83a2359

File tree

6 files changed

+43
-45
lines changed

6 files changed

+43
-45
lines changed

example/src/components/ReactNavigationDetailScreen.tsx

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,29 @@
11
import * as React from 'react';
22
import { Button, Text } from 'react-native';
3-
import type { ScreenProps } from '../types/Navigation';
3+
import type { Routes } from '../types/Navigation';
44
import { SafeAreaViewVisualizer } from './SafeAreaViewVisualizer';
5+
import { useNavigation } from '@react-navigation/native';
6+
import { NativeStackNavigationProp } from '@react-navigation/native-stack';
57

6-
export default function ReactNavigationDetailScreen({
7-
navigation,
8-
}: ScreenProps<'Details'>) {
9-
const isV5 = typeof navigation.setOptions === 'function';
8+
export default function ReactNavigationDetailScreen() {
109
const [headerShown, setHeaderShown] = React.useState(true);
10+
const { navigate, goBack, setOptions, push } =
11+
useNavigation<NativeStackNavigationProp<Routes>>();
12+
13+
const isV5 = typeof setOptions === 'function';
1114

1215
React.useLayoutEffect(() => {
1316
if (isV5) {
14-
navigation.setOptions({ headerShown });
17+
setOptions({ headerShown });
1518
}
16-
}, [navigation, isV5, headerShown]);
19+
}, [setOptions, isV5, headerShown]);
1720

1821
return (
1922
<SafeAreaViewVisualizer>
2023
<Text>Details Screen</Text>
21-
<Button
22-
title="Go to Details... again"
23-
onPress={() => navigation.push('Details')}
24-
/>
25-
<Button title="Go to Home" onPress={() => navigation.navigate('Home')} />
26-
<Button title="Go back" onPress={() => navigation.goBack()} />
24+
<Button title="Go to Details... again" onPress={() => push('Details')} />
25+
<Button title="Go to Home" onPress={() => navigate('Home')} />
26+
<Button title="Go back" onPress={() => goBack()} />
2727
{isV5 && (
2828
<Button
2929
title="Toggle header"

example/src/components/ReactNavigationHomeScreen.tsx

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,20 @@
11
import * as React from 'react';
22
import { Button, Text } from 'react-native';
3-
import type { ScreenProps } from '../types/Navigation';
3+
import type { Routes } from '../types/Navigation';
44
import { SafeAreaViewVisualizer } from './SafeAreaViewVisualizer';
5+
import { useNavigation } from '@react-navigation/native';
6+
import { NativeStackNavigationProp } from '@react-navigation/native-stack';
7+
8+
export default function ReactNavigationHomeScreen() {
9+
const { navigate, push } = useNavigation<NativeStackNavigationProp<Routes>>();
510

6-
export default function ReactNavigationHomeScreen({
7-
navigation,
8-
}: ScreenProps<'Home'>) {
911
return (
1012
<SafeAreaViewVisualizer>
1113
<Text>Home Screen</Text>
12-
<Button
13-
title="Go to Details"
14-
onPress={() => navigation.navigate('Details')}
15-
/>
14+
<Button title="Go to Details" onPress={() => navigate('Details')} />
1615
<Button
1716
title="Go to Modal Details"
18-
onPress={() => navigation.push('ModalDetails')}
17+
onPress={() => push('ModalDetails')}
1918
/>
2019
</SafeAreaViewVisualizer>
2120
);

example/src/components/ReactNavigationModalDetailScreen.tsx

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,28 @@
11
import * as React from 'react';
22
import { Button, Text } from 'react-native';
3-
import type { ScreenProps } from '../types/Navigation';
3+
import type { Routes } from '../types/Navigation';
44
import { SafeAreaViewVisualizer } from './SafeAreaViewVisualizer';
5+
import { useNavigation } from '@react-navigation/native';
6+
import { NativeStackNavigationProp } from '@react-navigation/native-stack';
57

6-
export default function ReactNavigationModalDetailScreen({
7-
navigation,
8-
}: ScreenProps<'ModalDetails'>) {
8+
export default function ReactNavigationModalDetailScreen() {
99
const [headerShown, setHeaderShown] = React.useState(false);
10+
const { navigate, goBack, setOptions, push } =
11+
useNavigation<NativeStackNavigationProp<Routes>>();
1012

1113
React.useLayoutEffect(() => {
12-
navigation.setOptions({ headerShown });
13-
}, [navigation, headerShown]);
14+
setOptions({ headerShown });
15+
}, [setOptions, headerShown]);
1416

1517
return (
1618
<SafeAreaViewVisualizer>
1719
<Text>Modal Details Screen</Text>
1820
<Button
1921
title="Go to Modal Details... again"
20-
onPress={() => navigation.push('ModalDetails')}
22+
onPress={() => push('ModalDetails')}
2123
/>
22-
<Button title="Go to Home" onPress={() => navigation.navigate('Home')} />
23-
<Button title="Go back" onPress={() => navigation.goBack()} />
24+
<Button title="Go to Home" onPress={() => navigate('Home')} />
25+
<Button title="Go back" onPress={() => goBack()} />
2426
<Button
2527
title="Toggle header"
2628
onPress={() => {

example/src/components/ReactNavigationSettingsScreen.tsx

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,20 @@
11
import * as React from 'react';
22
import { Button, Text } from 'react-native';
3-
import type { ScreenProps } from '../types/Navigation';
3+
import type { Routes } from '../types/Navigation';
44
import { SafeAreaViewVisualizer } from './SafeAreaViewVisualizer';
5+
import { NativeStackNavigationProp } from '@react-navigation/native-stack';
6+
import { useNavigation } from '@react-navigation/native';
7+
8+
export default function ReactNavigationSettingsScreen() {
9+
const { navigate, push } = useNavigation<NativeStackNavigationProp<Routes>>();
510

6-
export default function ReactNavigationSettingsScreen({
7-
navigation,
8-
}: ScreenProps<'Settings'>) {
911
return (
1012
<SafeAreaViewVisualizer>
1113
<Text>Settings Screen</Text>
12-
<Button
13-
title="Go to Details"
14-
onPress={() => navigation.navigate('Details')}
15-
/>
14+
<Button title="Go to Details" onPress={() => navigate('Details')} />
1615
<Button
1716
title="Go to Modal Details"
18-
onPress={() => navigation.push('ModalDetails')}
17+
onPress={() => push('ModalDetails')}
1918
/>
2019
</SafeAreaViewVisualizer>
2120
);

example/src/components/withProvider.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import * as React from 'react';
22
import { SafeAreaProvider } from 'react-native-safe-area-context';
33

4-
export function withProvider<T>(Component: React.ComponentType<T>) {
4+
export function withProvider<T extends object>(
5+
Component: React.ComponentType<T>,
6+
) {
57
return function WrappedScreen(props: T) {
68
return (
79
<SafeAreaProvider>

example/src/types/Navigation.tsx

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
1-
import type { StackScreenProps } from '@react-navigation/stack';
2-
31
export type Routes = {
42
Home: undefined;
53
Details: undefined;
64
ModalDetails: undefined;
75
Settings: undefined;
86
};
9-
10-
export type ScreenProps<T extends keyof Routes> = StackScreenProps<Routes, T>;

0 commit comments

Comments
 (0)