1、前提
安装好react navigation、bottomTabs、stack
2、效果图
3、测试代码
import {createBottomTabNavigator} from '@react-navigation/bottom-tabs';
import {NavigationContainer} from '@react-navigation/native';
import {
createNativeStackNavigator,
NativeStackNavigationProp,
} from '@react-navigation/native-stack';
import React from 'react';
import {View, Text, Button, StyleSheet} from 'react-native';
// 创建Stack
const Stack = createNativeStackNavigator();
// 创建底部Tab
const Tab = createBottomTabNavigator();
// 创建样式
const style = StyleSheet.create({
container: {
flex: 1,
alignItems: 'center',
justifyContent: 'center',
},
});
type RootStackParamList = {
Profile: undefined;
Detail: {
id: number;
screen?: string;
};
};
function Feed({
navigation,
}: {
navigation: NativeStackNavigationProp<RootStackParamList>;
}) {
return (
<View style={style.container}>
<Text>Home Screen</Text>
<Button
title="Go to Profile"
onPress={() => navigation.navigate('Profile')}
/>
</View>
);
}
function Messages() {
return (
<View>
<Text>Messages</Text>
</View>
);
}
function Settings() {
return (
<View>
<Text>Settings</Text>
</View>
);
}
function Profile() {
return (
<View>
<Text>Profile</Text>
</View>
);
}
function Home() {
return (
<Tab.Navigator>
<Tab.Screen
name="Feed"
component={Feed}
options={{
tabBarLabel: '新闻',
headerTitle: '新闻',
}}
/>
<Tab.Screen
name="Messages"
component={Messages}
options={{
tabBarLabel: '消息',
headerTitle: '消息',
}}
/>
</Tab.Navigator>
);
}
function App() {
return (
<NavigationContainer>
<Stack.Navigator>
<Stack.Screen
name="Home"
component={Home}
// 隐藏bottomTabs默认的顶部标题栏
options={{headerShown: false}}
/>
<Stack.Screen name="Profile" component={Profile} />
<Stack.Screen name="Settings" component={Settings} />
</Stack.Navigator>
</NavigationContainer>
);
}
export default App;