import React from 'react';
import { Button, Text, View } from 'react-native';
import { NavigationContainer } from '@react-navigation/native';
import { createNativeStackNavigator } from '@react-navigation/native-stack';
function HomeScreen({ navigation }) {
return (
<View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}>
<Button title="Go to Details" onPress={() => navigation.navigate('Details')} />
</View>
);
}
function DetailsScreen() {
return (
<View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}>
<Text>Details Screen</Text>
</View>
);
}
const Stack = createNativeStackNavigator();
export default function App() {
return (
<NavigationContainer>
<Stack.Navigator>
<Stack.Screen name="Home" component={HomeScreen} />
<Stack.Screen name="Details" component={DetailsScreen} />
</Stack.Navigator>
</NavigationContainer>
);
}This solution shows how to install and set up React Navigation for a simple two-screen app.
First, you install the required packages:
npm install @react-navigation/native @react-navigation/native-stack react-native-screens react-native-safe-area-context react-native-gesture-handler react-native-reanimated
Then, you import NavigationContainer which manages navigation state, and createNativeStackNavigator to create a stack of screens.
The HomeScreen has a button that calls navigation.navigate('Details') to go to the Details screen.
The DetailsScreen just shows some text.
The App component wraps the stack navigator inside the navigation container.
This setup follows React Navigation's official guidelines and creates a working navigation flow.