import * as React from 'react';
import { Button, Text, View } from 'react-native';
import { NavigationContainer } from '@react-navigation/native';
import { createStackNavigator } from '@react-navigation/stack';
const Stack = createStackNavigator();
const linking = {
prefixes: ['myapp://'],
config: {
screens: {
Home: '',
Details: 'details'
}
}
};
function HomeScreen({ navigation }) {
return (
<View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}>
<Text>Welcome!</Text>
<Button title="Go to Details" onPress={() => navigation.navigate('Details')} />
</View>
);
}
function DetailsScreen() {
return (
<View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}>
<Text>This is the Details Screen</Text>
</View>
);
}
export default function App() {
return (
<NavigationContainer linking={linking} fallback={<Text>Loading...</Text>}>
<Stack.Navigator>
<Stack.Screen name="Home" component={HomeScreen} />
<Stack.Screen name="Details" component={DetailsScreen} />
</Stack.Navigator>
</NavigationContainer>
);
}
We added a Button on the HomeScreen that navigates to the DetailsScreen when pressed. This lets users move between screens inside the app.
To support deep linking, we defined a linking configuration object. It tells React Navigation to listen for URLs starting with myapp://. The path details maps to the Details screen.
We passed this linking config to NavigationContainer. Now, if the app is opened with the URL myapp://details, it will open the DetailsScreen directly.
This setup helps users open specific parts of the app from outside, like from a browser or another app.