ตัวนำทางแบบลิ้นชักและตัวนำทางซ้อนกัน
ตั้งค่าตัวนำทางแบบลิ้นชัก ซ้อนตัวนำทางแบบสแตกไว้ในหนึ่งหน้าจอ และทำความเข้าใจว่าตัวนำทางซ้อนกันทำงานร่วมกับส่วนหัวและปุ่มย้อนกลับอย่างไร
ตัวนำทางแบบลิ้นชักและตัวนำทางซ้อนกัน เป็นบทเรียน React Native Academy ฟรีบน CoddyKit นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน React Native Academy และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส React Native Academy มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
What is a Drawer Navigator?
A drawer navigator shows a slide-out side menu (drawer) that the user can open by swiping from the left edge or tapping a hamburger icon. It is ideal for apps with many top-level sections that do not fit in a tab bar. React Navigation provides it via the @react-navigation/drawer package.
Installing the Drawer Package
The drawer navigator requires additional gesture and animation libraries. Install the drawer package and its peer dependencies. The drawer relies on react-native-gesture-handler and react-native-reanimated for smooth animations.
npm install @react-navigation/drawer
npx expo install react-native-gesture-handler react-native-reanimatedConfiguring Reanimated Babel Plugin
React Native Reanimated requires a Babel plugin to work. Add 'react-native-reanimated/plugin' as the last entry in your babel.config.js plugins array. Restart Metro bundler after making this change to clear the cache.
// babel.config.js
module.exports = function(api) {
api.cache(true);
return {
presets: ['babel-preset-expo'],
plugins: ['react-native-reanimated/plugin'],
};
};Creating a Drawer Navigator
Use createDrawerNavigator() and register screens with Drawer.Screen components. Each screen appears as a menu item in the drawer. The drawer also renders a header with a hamburger menu icon that opens the drawer when tapped.
import { createDrawerNavigator } from '@react-navigation/drawer';
const Drawer = createDrawerNavigator();
function MyDrawer() {
return (
<Drawer.Navigator initialRouteName='Home'>
<Drawer.Screen name='Home' component={HomeScreen} />
<Drawer.Screen name='Settings' component={SettingsScreen} />
<Drawer.Screen name='About' component={AboutScreen} />
</Drawer.Navigator>
);
}Opening and Closing the Drawer
From any screen inside the drawer navigator, call navigation.openDrawer() to open the side menu, navigation.closeDrawer() to close it, and navigation.toggleDrawer() to toggle between open and closed. Users can also swipe from the edge or tap the backdrop to close it.
function HomeScreen({ navigation }) {
return (
<Button
title='Open Menu'
onPress={() => navigation.openDrawer()}
/>
);
}Customizing Drawer Screen Options
Use the drawerIcon option on each Drawer.Screen to add an icon next to the menu item. Use drawerLabel to override the display name. These options follow the same pattern as tab navigator options, giving you a consistent API across navigators.
<Drawer.Screen
name='Home'
component={HomeScreen}
options={{
drawerLabel: 'Dashboard',
drawerIcon: ({ color, size }) => (
<Ionicons name='home-outline' size={size} color={color} />
),
}}
/>What Are Nested Navigators?
Nested navigators occur when you place one navigator inside a screen of another navigator. For example, a drawer navigator can contain a tab navigator, which can itself contain stack navigators inside each tab. This is how complex apps structure their navigation hierarchy.
Nesting a Stack Inside a Drawer Screen
To give each drawer section its own navigation history, use a Stack.Navigator as the component for a Drawer.Screen. The stack's header and the drawer header may conflict — set headerShown: false on the drawer to hide its header and rely on the stack header.
function HomeStack() {
return (
<Stack.Navigator>
<Stack.Screen name='Home' component={HomeScreen} />
<Stack.Screen name='Details' component={DetailsScreen} />
</Stack.Navigator>
);
}
<Drawer.Screen
name='HomeSection'
component={HomeStack}
options={{ headerShown: false }}
/>Navigation Across Nested Navigators
When navigating across nested navigators (e.g., from a screen inside a tab to a screen in another tab's stack), use the full screen name and navigator target. React Navigation resolves the correct navigator. You may also navigate to a parent navigator's screen using navigation.navigate('ParentScreen', { screen: 'ChildScreen' }).
// Navigate to a screen nested inside a different tab
navigation.navigate('ProfileTab', {
screen: 'EditProfile',
params: { userId: 5 },
});The Back Button and Nested Stacks
When a stack is nested inside a tab or drawer, the back button in the stack header only pops the stack — it does not switch tabs or close the drawer. Each navigator manages its own history independently. This is intentional and matches the expected behavior users experience in native apps.
Custom Drawer Content
Replace the default drawer menu with a fully custom component using the drawerContent prop on Drawer.Navigator. Your custom component receives navigation and state props and can display a user avatar, subscription info, or any other content above or below the menu items.
function CustomDrawer(props) {
return (
<DrawerContentScrollView {...props}>
<Image source={{ uri: user.avatar }} style={styles.avatar} />
<Text>{user.name}</Text>
<DrawerItemList {...props} />
</DrawerContentScrollView>
);
}
<Drawer.Navigator drawerContent={(props) => <CustomDrawer {...props} />}>Quick Check
Test your understanding of Drawer Navigator and nested navigators from this lesson.
Lesson Recap
In this lesson you learned: create a drawer navigator with createDrawerNavigator, open and close the drawer programmatically with navigation.openDrawer/closeDrawer, and nest navigators by using a navigator component as a screen's component prop. Next up we explore the FlatList component for efficiently rendering large data sets.
เรียนรู้ JavaScript ด้วย AI tutor — ฟรี
เขียนและเรียกใช้โค้ดจริงในเบราว์เซอร์ของคุณ รับความช่วยเหลือทันทีจาก AI tutor 24/7 และเรียนรู้ต่อจากที่คุณหยุดบนเว็บหรือในแอป
- คอร์ส
- 30
- บทเรียน
- 120
คำถามที่พบบ่อย
บทเรียน “ตัวนำทางแบบลิ้นชักและตัวนำทางซ้อนกัน” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “ตัวนำทางแบบลิ้นชักและตัวนำทางซ้อนกัน” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส React Native Academy ให้อัปเกรดเป็น CoddyKit PRO คอร์ส React Native Academy มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “ตัวนำทางแบบลิ้นชักและตัวนำทางซ้อนกัน”
ตั้งค่าตัวนำทางแบบลิ้นชัก ซ้อนตัวนำทางแบบสแตกไว้ในหนึ่งหน้าจอ และทำความเข้าใจว่าตัวนำทางซ้อนกันทำงานร่วมกับส่วนหัวและปุ่มย้อนกลับอย่างไร คุณปฏิบัติ React Native Academy ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน React Native Academy หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน React Native Academy บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน
บทเรียน “ตัวนำทางแบบลิ้นชักและตัวนำทางซ้อนกัน” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน React Native Academy นี้ได้ไหม
ได้ บทเรียน React Native Academy ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- การติดตั้งและกำหนดค่า React Navigation
- การนำทางระหว่างหน้าจอและการส่งพารามิเตอร์
- ตัวนำทางแท็บด้านล่าง
- ตัวนำทางแบบลิ้นชักและตัวนำทางซ้อนกัน