import React from 'react';
import { Button, View, Text } from 'react-native';
import { NavigationContainer } from '@react-navigation/native';
import { createNativeStackNavigator } from '@react-navigation/native-stack';
const Stack = createNativeStackNavigator();
function HomeScreen({ navigation }) {
return (
<View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}>
<Text>Home Screen</Text>
<Button title="Go to Details" onPress={() => navigation.navigate('Details')} />
</View>
);
}
function DetailsScreen({ navigation }) {
return (
<View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}>
<Text>Details Screen</Text>
<Button title="Go Back" onPress={() => navigation.goBack()} />
</View>
);
}
export default function App() {
return (
<NavigationContainer>
<Stack.Navigator>
<Stack.Screen name="Home" component={HomeScreen} />
<Stack.Screen name="Details" component={DetailsScreen} />
</Stack.Navigator>
</NavigationContainer>
);
}This app uses React Navigation to manage screen flow. We create a stack navigator that holds two screens: Home and Details.
The HomeScreen has a button labeled 'Go to Details'. When pressed, it calls navigation.navigate('Details') to move to the Details screen.
The DetailsScreen has a button labeled 'Go Back'. When pressed, it calls navigation.goBack() to return to the previous screen, which is Home.
This shows how navigation manages the flow between screens, letting users move forward and backward in the app.