此文章为ReactNavigation导航库5.0版本的第4篇,前几篇系列文章以下: React Navigation5.0系列一:StackNavigator的使用 React Navigation5.0系列二:TabNavigation的使用 React Navigation5.0系列三:Drawer navigation的使用 此前几篇系列文章,主要讲了StackNavigator, TavNavigation以及Drawer Navigation的使用讲解,现实中每每是不一样的导航组件组合进行使用的,本篇文章主要讲解导航的嵌套使用及注意事项。 @[toc]html
// 设置页面 const SettingsScreen = ({ navigation }) => { return ( <View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}> <Text>SettingScreen</Text> <Button title="Go to Details" onPress={() => navigation.navigate('Detail')} /> </View> ) } // 首页 const HomeScreen = ({ navigation }) => { return ( <View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}> <Text>HomeScreen</Text> <Button title="Go to Details" onPress={() => navigation.navigate('Detail')} /> </View> ) } // 详情页 const DetailScreen = ({ navigation }) => { return ( <View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}> <Text>DetailScreen</Text> <Button title="Go to Detail Again" // onPress={() => navigation.navigate('Detail')} onPress={() => navigation.push('Detail')} /> <Button title="Go to Home" onPress={() => navigation.navigate('Home')} /> <Button title="Go back" onPress={() => navigation.goBack()} /> <Button title="Go back to first screen in stack" onPress={() => navigation.popToTop()} /> </View> ) }
const Tab = createBottomTabNavigator(); // 选项卡页签tab navigator 实例 const RootStack = createStackNavigator(); // 堆栈stack 实例 const Drawer = createDrawerNavigator(); // 抽屉drawer实例
function IconWithBadge({ icon, badgeCount, size }) { return ( <View style={{ width: 24, height: 24, margin: 5 }}> <Image source={icon} style={{ width: size, height: size }} /> {badgeCount > 0 && ( <View style={{ // On React Native < 0.57 overflow outside of parent will not work on Android, see https://git.io/fhLJ8 position: 'absolute', right: -6, top: -3, backgroundColor: 'red', borderRadius: 6, width: 12, height: 12, justifyContent: 'center', alignItems: 'center', }} > <Text style={{ color: 'white', fontSize: 10, fontWeight: 'bold' }}> {badgeCount} </Text> </View> )} </View> ); } function HomeIconWithBadge(props) { // You should pass down the badgeCount in some other ways like React Context API, Redux, MobX or event emitters. return <IconWithBadge {...props} badgeCount={3} />; } const TabScreen = () => { return ( <Tab.Navigator headerMode='none' screenOptions={({ route }) => ({ tabBarIcon: ({ focused, color, size }) => { if (route.name === 'Home') { return ( <HomeIconWithBadge icon={ focused ? HomeIconActive : HomeIconNormal } size={size} color={color} /> ); } else if (route.name === 'Settings') { return ( <Image source={focused ? WorkIconActive : WorkIconNormal} style={{width: size, height: size}} /> ); } }, })} tabBarOptions={{ activeTintColor: 'tomato', inactiveTintColor: 'gray', }} > <Tab.Screen name="Home" component={HomeScreen} /> <Tab.Screen name="Settings" component={SettingsScreen} /> </Tab.Navigator> ) }
const rootRouteScreen = () => { return (<RootStack.Navigator initialRouteName={'TabNav'}> <RootStack.Screen name='TabNav' component={TabScreen} /> <RootStack.Screen name="Detail" component={DetailScreen} /> </RootStack.Navigator> ) }
const App = () => { return ( <NavigationContainer> <Drawer.Navigator initialRouteName="Home" drawerType='slide' drawerContent={(props) => <CustomDrawerContent {...props} />} > <Drawer.Screen name='root' component={rootRouteScreen} /> <Drawer.Screen name='Setting' component={SettingsScreen} /> </Drawer.Navigator> </NavigationContainer> ); }
最后咱们来看一下效果 (type-bilibili)(url-https://player.bilibili.com/player.html?aid=625504500)(image-https://ss.csdn.net/p?http://i2.hdslb.com/bfs/archive/23d0e36df0378cbd3a16ab49d57a746db853648c.jpg)(title-ReactNative官方推荐导航ReactNavigation5.0版导航嵌套)]react
非嵌套导航使用以下的方式进行传递和接收参数以下方式git
// 传递参数 <DrawerItem label="Help" onPress={() => props.navigation.navigate('Detail',{ itemId: 86, otherParam: 'anything you want here', })} /> // 接收参数 const DetailScreen = ({ route, navigation }) => { const { itemId } = route.params; const { otherParam } = route.params; return ( <View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}> <Text>DetailScreen: {itemId} {otherParam}</Text> </View> ) }
在嵌套导航中进行参数传递,须要额外加一个params 的keygithub
navigation.navigate('Root', { screen: 'Settings', params: { user: 'jane' }, });
建议将嵌套作到最少,应该尝试采用尽量少的嵌套来实现你的业务需求,由于多层嵌套会致使以下几个问题:微信
官网也列举了一下常见的问题,朋友们在集成过程当中遇到问题能够经过以下地址看一下 Troubleshooting, 固然还有Github上的issues 地址:关于React-Navigation的问题ide
以为文章不错的,给我点个赞哇,关注一下呗! 技术交流可关注微信公众号【君伟说】,加我好友一块儿探讨 微信交流群:加好友(备注技术交流)邀你入群,抱团学习共进步性能