|
| 1 | +import React from 'react'; |
| 2 | +import { NavigationContainer, NavigationContext, ParamListBase } from '@react-navigation/native'; |
| 3 | +import { createNativeStackNavigator, NativeStackNavigationProp } from '@react-navigation/native-stack'; |
| 4 | +import { View, StyleSheet, Button, Pressable, Text } from 'react-native'; |
| 5 | + |
| 6 | +type NavProp = { |
| 7 | + navigation: NativeStackNavigationProp<ParamListBase>; |
| 8 | +}; |
| 9 | + |
| 10 | +const Stack = createNativeStackNavigator(); |
| 11 | + |
| 12 | +function FirstScreen({ navigation }: NavProp) { |
| 13 | + const navigateToSecond = () => { |
| 14 | + navigation.navigate('Second'); |
| 15 | + }; |
| 16 | + return ( |
| 17 | + <View style={[styles.redbox, styles.centeredView]}> |
| 18 | + <Button title="Navigate to Second" onPress={navigateToSecond} /> |
| 19 | + <PressableWithHitSlop /> |
| 20 | + </View> |
| 21 | + ); |
| 22 | +} |
| 23 | + |
| 24 | +function SecondScreen({ navigation }: NavProp) { |
| 25 | + const navigateToFirst = () => { |
| 26 | + navigation.navigate('First'); |
| 27 | + }; |
| 28 | + |
| 29 | + return ( |
| 30 | + <View style={[styles.greenbox, styles.centeredView]}> |
| 31 | + <Button title="Navigate to First" onPress={navigateToFirst} /> |
| 32 | + </View> |
| 33 | + ); |
| 34 | +} |
| 35 | + |
| 36 | +function HeaderLeft() { |
| 37 | + const onPressCallback = () => { |
| 38 | + console.log('HeaderLeft onPressCallback invoked'); |
| 39 | + }; |
| 40 | + |
| 41 | + return ( |
| 42 | + <Pressable style={[styles.bluebox]} hitSlop={12} onPress={onPressCallback}> |
| 43 | + <Text style={{ color: 'white' }}>Press me</Text> |
| 44 | + </Pressable> |
| 45 | + ); |
| 46 | +} |
| 47 | + |
| 48 | +function PressableWithHitSlop() { |
| 49 | + const onPressCallback = () => { |
| 50 | + console.log('PressableWithHitSlop onPressCallback invoked'); |
| 51 | + }; |
| 52 | + |
| 53 | + return ( |
| 54 | + <View |
| 55 | + style={{ |
| 56 | + padding: 12, |
| 57 | + margin: -12, |
| 58 | + backgroundColor: 'yellow', |
| 59 | + }}> |
| 60 | + <Pressable |
| 61 | + style={[styles.greenbox]} |
| 62 | + hitSlop={12} |
| 63 | + onPress={onPressCallback}> |
| 64 | + <Text style={{ color: 'white' }}>Press me</Text> |
| 65 | + </Pressable> |
| 66 | + </View> |
| 67 | + ); |
| 68 | +} |
| 69 | + |
| 70 | +export default function App() { |
| 71 | + return ( |
| 72 | + <NavigationContainer> |
| 73 | + <Stack.Navigator> |
| 74 | + <Stack.Screen |
| 75 | + name="First" |
| 76 | + component={FirstScreen} |
| 77 | + options={{ |
| 78 | + headerLeft: () => HeaderLeft(), |
| 79 | + headerRight: () => PressableWithHitSlop(), |
| 80 | + }} |
| 81 | + /> |
| 82 | + <Stack.Screen name="Second" component={SecondScreen} /> |
| 83 | + </Stack.Navigator> |
| 84 | + </NavigationContainer> |
| 85 | + ); |
| 86 | +} |
| 87 | + |
| 88 | +const styles = StyleSheet.create({ |
| 89 | + redbox: { |
| 90 | + backgroundColor: 'red', |
| 91 | + }, |
| 92 | + greenbox: { |
| 93 | + backgroundColor: 'green', |
| 94 | + }, |
| 95 | + bluebox: { |
| 96 | + backgroundColor: 'blue', |
| 97 | + }, |
| 98 | + centeredView: { |
| 99 | + flex: 1, |
| 100 | + justifyContent: 'center', |
| 101 | + alignItems: 'center', |
| 102 | + }, |
| 103 | +}); |
0 commit comments