P.S: Most of the Youtube videos or articles on the web aren't using ReactNavigation v5, they are using older versions. Can someone show a dummy project when user can click button to navigate to a different screen (using StackNavigator) and DrawerNavigator as well to navigate between screens. The screens must have a class and a simple text, thats it. Thanks!
Stack Navigator provides a way for an app to transition between screens when each new screen is placed on top of a stack. In case of this Twitter clone, we will use it to transition from a screen displaying a feed of tweets to the screen showing details of a tweet. React Navigation v5 provides two implementations of a Stack Navigator
You don't have to be using @react-navigation/elements directly to use these options, they are just documented in that page. In addition to those, the following options are also supported in stack:
By default the stack navigator is configured to have the familiar iOS and Android look & feel: new screens slide in from the right on iOS, use OS default animation on Android. But the animations can be customized to match your needs.
The main difference between them is that JS-based stack re-implements animations and gestures while the native stack navigator relies on the platform primitives for animations and gestures. In this section, we will integrate React Native Paper Appbar and JS-based Stack Navigator.
You can have a basic setup like this where you have Home,Profile and Notification screens. Home and Profile are under a stack and notifications is a separate screen. Both the stack and the notification are placed under the drawer navigation.
Here I've used class components as thats your requirement but functional components are preferred as there are hooks like useNavigation provided by Navigation5 but there are workarounds to use these as well.
You can see the code below.
import * as React from 'react';
import { Button, View, Text } from 'react-native';
import { createDrawerNavigator } from '@react-navigation/drawer';
import { NavigationContainer } from '@react-navigation/native';
import { createStackNavigator } from '@react-navigation/stack';
class HomeScreen extends React.Component {
render() {
return (
<View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
<Text>Home Screen</Text>
<Button
onPress={() => this.props.navigation.navigate('Notifications')}
title="Go to notifications"
/>
<Button
onPress={() => this.props.navigation.navigate('Profile')}
title="Go to Profile"
/>
</View>
);
}
}
class ProfileScreen extends React.Component {
render() {
return (
<View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
<Text>Profile Screen</Text>
<Button
onPress={() => this.props.navigation.navigate('Notifications')}
title="Go to notifications"
/>
</View>
);
}
}
class NotificationsScreen extends React.Component {
render() {
return (
<View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
<Text>Notifications Screen</Text>
<Button
onPress={() => this.props.navigation.goBack()}
title="Go back home"
/>
</View>
);
}
}
const Stack = createStackNavigator();
function HomeStack() {
return (
<Stack.Navigator>
<Stack.Screen name="Home" component={HomeScreen} />
<Stack.Screen name="Profile" component={ProfileScreen} />
</Stack.Navigator>
);
}
const Drawer = createDrawerNavigator();
export default class App extends React.Component {
render() {
return (
<NavigationContainer>
<Drawer.Navigator initialRouteName="Home">
<Drawer.Screen name="Home" component={HomeStack} />
<Drawer.Screen name="Notifications" component={NotificationsScreen} />
</Drawer.Navigator>
</NavigationContainer>
);
}
}
You can try out the code in the below snack as well. https://snack.expo.io/@guruparan/navsample
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With