|
| 1 | +import React, { useState } from 'react'; |
| 2 | +import { createNativeStackNavigator } from '@react-navigation/native-stack'; |
| 3 | +import { createMaterialTopTabNavigator } from '@react-navigation/material-top-tabs'; |
| 4 | + |
| 5 | +import { View, Text, Button } from 'react-native'; |
| 6 | + |
| 7 | +function Tab1() { |
| 8 | + return ( |
| 9 | + <View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}> |
| 10 | + <Text>Tab 1</Text> |
| 11 | + </View> |
| 12 | + ); |
| 13 | +} |
| 14 | + |
| 15 | +function Tab2(props: any) { |
| 16 | + return ( |
| 17 | + <View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}> |
| 18 | + <Button title="Logout" onPress={props.onLogout} /> |
| 19 | + </View> |
| 20 | + ); |
| 21 | +} |
| 22 | + |
| 23 | +const PreAuthScreen = (props: any) => { |
| 24 | + return ( |
| 25 | + <View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}> |
| 26 | + <Button title="Login" onPress={() => props.setIsSignedIn(true)} /> |
| 27 | + </View> |
| 28 | + ); |
| 29 | +}; |
| 30 | + |
| 31 | +const PostAuthScreen = (props: any) => { |
| 32 | + const { Navigator, Screen } = createMaterialTopTabNavigator(); |
| 33 | + const onLogout = () => { |
| 34 | + setTimeout(() => { |
| 35 | + props.setIsSignedIn(false); |
| 36 | + }, 0); |
| 37 | + }; |
| 38 | + |
| 39 | + return ( |
| 40 | + <View style={{ flex: 1 }}> |
| 41 | + <Navigator> |
| 42 | + <Screen name="Tab1" component={Tab1} /> |
| 43 | + <Screen name="Tab2"> |
| 44 | + {(props: any) => <Tab2 {...props} onLogout={onLogout} />} |
| 45 | + </Screen> |
| 46 | + </Navigator> |
| 47 | + </View> |
| 48 | + ); |
| 49 | +}; |
| 50 | + |
| 51 | +export function MaterialTopBarExample() { |
| 52 | + const { Screen, Navigator } = createNativeStackNavigator(); |
| 53 | + const [isSignedIn, setIsSignedIn] = useState(false); |
| 54 | + |
| 55 | + return ( |
| 56 | + <Navigator> |
| 57 | + {!isSignedIn ? ( |
| 58 | + <Screen name="Pre Auth Screen"> |
| 59 | + {(props: any) => ( |
| 60 | + <PreAuthScreen {...props} setIsSignedIn={setIsSignedIn} /> |
| 61 | + )} |
| 62 | + </Screen> |
| 63 | + ) : ( |
| 64 | + <Screen name="Post Auth Screen"> |
| 65 | + {(props: any) => ( |
| 66 | + <PostAuthScreen {...props} setIsSignedIn={setIsSignedIn} /> |
| 67 | + )} |
| 68 | + </Screen> |
| 69 | + )} |
| 70 | + </Navigator> |
| 71 | + ); |
| 72 | +} |
0 commit comments