|
| 1 | +import React from 'react'; |
| 2 | +// import { NavigationContainer } from '@react-navigation/native'; |
| 3 | +// import { Pressable, Text, View, StyleSheet, Button } from 'react-native'; |
| 4 | +// import { createNativeStackNavigator } from '@react-navigation/native-stack'; |
| 5 | +// import { GestureHandlerRootView } from 'react-native-gesture-handler'; |
| 6 | +// |
| 7 | +// const styles = StyleSheet.create({ |
| 8 | +// button: { |
| 9 | +// width: 42, |
| 10 | +// height: 42, |
| 11 | +// marginHorizontal: 5, |
| 12 | +// padding: 5, |
| 13 | +// borderRadius: 20, |
| 14 | +// justifyContent: 'center', |
| 15 | +// alignItems: 'center', |
| 16 | +// backgroundColor: 'blue', |
| 17 | +// }, |
| 18 | +// buttonsView: { |
| 19 | +// flexDirection: 'row', |
| 20 | +// borderWidth: 1, |
| 21 | +// }, |
| 22 | +// }); |
| 23 | +// |
| 24 | +// const Stack = createNativeStackNavigator(); |
| 25 | +// |
| 26 | +// function MyStack() { |
| 27 | +// return ( |
| 28 | +// <Stack.Navigator |
| 29 | +// screenOptions={{ |
| 30 | +// headerShown: true, |
| 31 | +// headerBackButtonDisplayMode: 'minimal', |
| 32 | +// }}> |
| 33 | +// <Stack.Screen name="Home" component={HomeScreen} /> |
| 34 | +// <Stack.Screen name="Profile" component={HomeScreen} /> |
| 35 | +// </Stack.Navigator> |
| 36 | +// ); |
| 37 | +// } |
| 38 | +// |
| 39 | +// const HomeScreen = ({ navigation }: any) => { |
| 40 | +// const [secondButtonShown, setSecondButtonShown] = React.useState(true); |
| 41 | +// const [thirdButtonShown, setThirdButtonShown] = React.useState(true); |
| 42 | +// const [showAllButtons, setShowAllButtons] = React.useState(true); |
| 43 | +// |
| 44 | +// const headerRight = React.useCallback(() => { |
| 45 | +// return ( |
| 46 | +// <> |
| 47 | +// <Pressable style={styles.button} onPress={() => console.log(1)}> |
| 48 | +// <Text>1</Text> |
| 49 | +// </Pressable> |
| 50 | +// {secondButtonShown && ( |
| 51 | +// <Pressable style={styles.button} onPress={() => console.log(2)}> |
| 52 | +// <Text>2</Text> |
| 53 | +// </Pressable> |
| 54 | +// )} |
| 55 | +// {thirdButtonShown && ( |
| 56 | +// <Pressable style={styles.button} onPress={() => console.log(3)}> |
| 57 | +// <Text>3</Text> |
| 58 | +// </Pressable> |
| 59 | +// )} |
| 60 | +// </> |
| 61 | +// ); |
| 62 | +// }, [secondButtonShown, thirdButtonShown]); |
| 63 | +// |
| 64 | +// React.useLayoutEffect(() => { |
| 65 | +// navigation.setOptions({ |
| 66 | +// headerStyle: { backgroundColor: 'pink' }, |
| 67 | +// headerRight: () => ( |
| 68 | +// <View |
| 69 | +// style={[styles.buttonsView, !showAllButtons && { display: 'none' }]}> |
| 70 | +// {headerRight()} |
| 71 | +// <Pressable style={styles.button} onPress={() => console.log('D')}> |
| 72 | +// <Text>[D]</Text> |
| 73 | +// </Pressable> |
| 74 | +// </View> |
| 75 | +// ), |
| 76 | +// }); |
| 77 | +// }, [navigation, headerRight, showAllButtons]); |
| 78 | +// |
| 79 | +// return ( |
| 80 | +// <View> |
| 81 | +// <Text>Home Screen</Text> |
| 82 | +// <Button |
| 83 | +// title="Toggle 2nd button" |
| 84 | +// onPress={() => setSecondButtonShown(p => !p)} |
| 85 | +// /> |
| 86 | +// <Button |
| 87 | +// title="Toggle 3rd button" |
| 88 | +// onPress={() => setThirdButtonShown(p => !p)} |
| 89 | +// /> |
| 90 | +// <Button |
| 91 | +// title="Toggle All Right Buttons" |
| 92 | +// onPress={() => setShowAllButtons(p => !p)} |
| 93 | +// /> |
| 94 | +// </View> |
| 95 | +// ); |
| 96 | +// }; |
| 97 | +// |
| 98 | +// function App() { |
| 99 | +// return ( |
| 100 | +// <GestureHandlerRootView> |
| 101 | +// <NavigationContainer> |
| 102 | +// <MyStack /> |
| 103 | +// </NavigationContainer> |
| 104 | +// </GestureHandlerRootView> |
| 105 | +// ); |
| 106 | +// } |
| 107 | +// |
| 108 | +// export default App; |
| 109 | + |
| 110 | +import { |
| 111 | + createStaticNavigation, |
| 112 | + useNavigation, |
| 113 | +} from '@react-navigation/native'; |
| 114 | +import { createNativeStackNavigator } from '@react-navigation/native-stack'; |
| 115 | +import { Button, StyleSheet, Text, View } from 'react-native'; |
| 116 | +import { SafeAreaProvider } from 'react-native-safe-area-context'; |
| 117 | +import { useEffect } from 'react'; |
| 118 | + |
| 119 | +const RootStack = createNativeStackNavigator({ |
| 120 | + screens: { |
| 121 | + Home: { |
| 122 | + screen: HomeScreen, |
| 123 | + options: { |
| 124 | + headerRight: () => <Text style={styles.headerRight}>Hi</Text>, |
| 125 | + headerTitle: 'Longer Screen Title', |
| 126 | + }, |
| 127 | + }, |
| 128 | + }, |
| 129 | +}); |
| 130 | + |
| 131 | +const RootNavigation = createStaticNavigation(RootStack); |
| 132 | + |
| 133 | +export default function App() { |
| 134 | + return ( |
| 135 | + <SafeAreaProvider> |
| 136 | + <RootNavigation /> |
| 137 | + </SafeAreaProvider> |
| 138 | + ); |
| 139 | +} |
| 140 | + |
| 141 | +function HomeScreen() { |
| 142 | + const navigation = useNavigation(); |
| 143 | + |
| 144 | + return ( |
| 145 | + <View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}> |
| 146 | + <Button |
| 147 | + title={'Update Header'} |
| 148 | + onPress={() => { |
| 149 | + navigation.setOptions({ |
| 150 | + headerRight: () => ( |
| 151 | + <Text style={[styles.headerRight]}>Longer Header Right</Text> |
| 152 | + ), |
| 153 | + }); |
| 154 | + }} |
| 155 | + /> |
| 156 | + </View> |
| 157 | + ); |
| 158 | +} |
| 159 | + |
| 160 | +const styles = StyleSheet.create({ |
| 161 | + headerRight: { |
| 162 | + paddingHorizontal: 10, |
| 163 | + fontSize: 20, |
| 164 | + fontWeight: 'bold', |
| 165 | + }, |
| 166 | +}); |
0 commit comments