24
loading...
This website collects cookies to deliver better user experience
Profile
and the other I called List
. For fun, I did create a ListItem
component and fed a small list of groceries from a JS array of objects into the List
page, but it's basically just a list of text objects. In Profile
I just wrote my name.Home
page for the buttons to exist in. The other two screens are secondary screens, and Home
was going to be the home screen. When we install navigation the App
file will simply hold our navigation and routing, so it was important to create a Home
screen to store JSX in for the buttons.Button
component from React Native and created two, one for each secondary page. Here's what the template code from that file looked like for me:...
function HomeScreen({ navigation }) {
const { navigate } = navigation;
return (
<View>
<Text style={ styles.headerText } >
Home Screen
</Text>
<View style={ styles.buttonsContainer }>
<Button
title="Go to Profile Screen"
onPress={()=> navigate( 'Profile' )}
/>
<Button
title="Go to List Screen"
onPress={()=> navigate( 'List' )}
/>
</View>
</View>
)
};
...
App
file, we can set up some navigation since we have a component for the home screen view.npm install @react-navigation/native @react-navigation/native-stack
expo install react-native-screens react-native-safe-area-context
App
file we want to import the navigation modules....
import { NavigationContainer } from '@react-navigation/native';
import { createNativeStackNavigator } from '@react-navigation/native-stack';
const Stack = createNativeStackNavigator();
...
App
by enclosing everything in NavigationContainer
tags. Inside those tags, we create the stack and each screen within it:function App(){
return (
<NavigationContainer>
<Stack.Navigator>
<Stack.Screen name="Home" component={ HomeScreen } />
<Stack.Screen name="Profile" component={ ProfileScreen } />
<Stack.Screen name="List" component={ ListScreen } />
</Stack.Navigator>
</NavigationContainer>
)
};
onPress
props on the button elements sending users to different pages. To do this with React Navigation, we destructure out a navigation
prop that comes with each element using React Navigation. The navigate()
function that takes an argument sends us to the corret place in the stack we created earlier ourselves :)