|
import { useEffect } from "react"; |
|
import { StyleSheet, Text, View, TouchableOpacity } from "react-native"; |
|
|
|
import { useRoute, NavigationContainer } from "@react-navigation/native"; |
|
import { createBottomTabNavigator } from '@react-navigation/bottom-tabs'; |
|
import { createNativeStackNavigator } from "@react-navigation/native-stack"; |
|
|
|
const Stack = createNativeStackNavigator(); |
|
const Tab = createBottomTabNavigator(); |
|
|
|
function Tab1({ navigation }) { |
|
const route = useRoute(); |
|
useEffect(() => { |
|
const unsubscribe = navigation.addListener('focus', () => { |
|
console.log({route: route.name}); |
|
}); |
|
return unsubscribe; |
|
}, [navigation]); |
|
|
|
return ( |
|
<View style={{ flex: 1, alignItems: "center", justifyContent: "center" }}> |
|
<TouchableOpacity onPress={() => navigation.navigate("Home")}> |
|
<Text>Tab Home Screen</Text> |
|
</TouchableOpacity> |
|
<TouchableOpacity onPress={() => navigation.navigate("Next")}> |
|
<Text>Tab Next Screen</Text> |
|
</TouchableOpacity> |
|
</View> |
|
); |
|
} |
|
function Tab2({ navigation }) { |
|
const route = useRoute(); |
|
useEffect(() => { |
|
const unsubscribe = navigation.addListener('focus', () => { |
|
console.log({route: route.name}); |
|
}); |
|
return unsubscribe; |
|
}, [navigation]); |
|
|
|
return ( |
|
<View style={{ flex: 1, alignItems: "center", justifyContent: "center" }}> |
|
<TouchableOpacity onPress={() => navigation.navigate("Next")}> |
|
<Text>Tab 1 Screen</Text> |
|
</TouchableOpacity> |
|
</View> |
|
); |
|
} |
|
|
|
function NextScreen() { |
|
const route = useRoute(); |
|
|
|
return ( |
|
<Tab.Navigator |
|
screenOptions={{ |
|
headerShown: false, |
|
}} |
|
> |
|
<Tab.Screen name="Tab1" component={Tab1} /> |
|
<Tab.Screen name="Tab2" component={Tab2} /> |
|
</Tab.Navigator> |
|
); |
|
} |
|
|
|
function HomeScreen({ navigation }) { |
|
return ( |
|
<View style={{ flex: 1, alignItems: "center", justifyContent: "center" }}> |
|
<TouchableOpacity onPress={() => navigation.navigate("Next")}> |
|
<Text>Go To Next Screen</Text> |
|
</TouchableOpacity> |
|
</View> |
|
); |
|
} |
|
|
|
export default function App() { |
|
return ( |
|
<NavigationContainer> |
|
<Stack.Navigator> |
|
<Stack.Screen name="Home" component={HomeScreen} /> |
|
<Stack.Screen name="Next" component={NextScreen} /> |
|
</Stack.Navigator> |
|
</NavigationContainer> |
|
); |
|
} |
|
|
|
const styles = StyleSheet.create({ |
|
container: { |
|
flex: 1, |
|
backgroundColor: "#fff", |
|
alignItems: "center", |
|
justifyContent: "center", |
|
}, |
|
}); |