Rah*_*hul 5 react-native react-navigation expo react-navigation-drawer react-navigation-v5
PS:大多数 Youtube 视频或网络上的文章都没有使用 ReactNavigation v5,它们使用的是旧版本。当用户可以单击按钮导航到不同的屏幕(使用 StackNavigator)和 DrawerNavigator 以及在屏幕之间导航时,有人可以显示一个虚拟项目。屏幕必须有一个类和一个简单的文本,就是这样。谢谢!
您可以进行像这样的基本设置,其中有主页、个人资料和通知屏幕。主页和个人资料位于堆栈下,通知位于单独的屏幕上。堆栈和通知都放置在抽屉式导航下方。
在这里,我使用了类组件,因为这是您的要求,但功能组件是首选,因为有诸如 Navigation5 提供的 useNavigation 之类的钩子,但也有使用这些组件的解决方法。
您可以看到下面的代码。
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>
);
}
}
Run Code Online (Sandbox Code Playgroud)
您也可以尝试下面的小吃中的代码。 https://snack.expo.io/@guruparan/navsample
| 归档时间: |
|
| 查看次数: |
1643 次 |
| 最近记录: |