설치
#npm을 쓰는 경우라면 npm install @react-navigation/drawer #yarn을 쓰는 경우라면 yarn add @react-navigation/drawer
드로워 열고 닫기
명시적으로 여닫기
//드로워 열기 navigation.openDrawer(); //드로워 닫기 navigation.closeDrawer();
토글로 여닫기
//토글로 열고 닫기 navigation.toggleDrawer();
이 3개의 함수들은 사실
navigation.dispatch
로 action
을 디스패치 하는 것임.(그냥 알고만 있자)
//두 함수 동일 navigation.openDrawer(); navigation.dispatch(DrawerActions.openDrawer()); //두 함수 동일 navigation.closeDrawer(); navigation.dispatch(DrawerActions.closeDrawer()); //두 함수 동일 navigation.toggleDrawer(); navigation.dispatch(DrawerActions.toggleDrawer());
예시코드
기본 코드
import * as React from 'react'; import { Button, View } from 'react-native'; import { createDrawerNavigator } from '@react-navigation/drawer'; import { NavigationContainer } from '@react-navigation/native'; function HomeScreen({ navigation }) { return ( <View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}> <Button onPress={() => navigation.navigate('Notifications')} title="Go to notifications" /> </View> ); } function NotificationsScreen({ navigation }) { return ( <View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}> <Button onPress={() => navigation.goBack()} title="Go back home" /> </View> ); } const Drawer = createDrawerNavigator(); export default function App() { return ( <NavigationContainer> <Drawer.Navigator initialRouteName="Home"> <Drawer.Screen name="Home" component={HomeScreen} /> <Drawer.Screen name="Notifications" component={NotificationsScreen} /> </Drawer.Navigator> </NavigationContainer> ); }
커스텀 드로워
import * as React from 'react'; import { View, Text, Button } from 'react-native'; import { NavigationContainer } from '@react-navigation/native'; import { createDrawerNavigator, DrawerContentScrollView, DrawerItemList, DrawerItem, } from '@react-navigation/drawer'; function Feed({ navigation }) { return ( <View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}> <Text>Feed Screen</Text> <Button title="Open drawer" onPress={() => navigation.openDrawer()} /> <Button title="Toggle drawer" onPress={() => navigation.toggleDrawer()} /> </View> ); } function Notifications() { return ( <View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}> <Text>Notifications Screen</Text> </View> ); } function CustomDrawerContent(props) { return ( <DrawerContentScrollView {...props /* 스크롤 가능 */}> <DrawerItemList {...props} /*기존의 컴포넌트부터 생성하고 (Feed, Notifications)*//> <DrawerItem /* 추가 컴포넌트 추가함 */ label="Close drawer" onPress={() => props.navigation.closeDrawer()} /> <DrawerItem /* 추가 컴포넌트 추가함 */ label="Toggle drawer" onPress={() => props.navigation.toggleDrawer()} /> </DrawerContentScrollView> ); } const Drawer = createDrawerNavigator(); function MyDrawer() { return ( <Drawer.Navigator drawerContent={props => <CustomDrawerContent {...props} />}> <Drawer.Screen name="Feed" component={Feed} /> <Drawer.Screen name="Notifications" component={Notifications} /> </Drawer.Navigator> ); } export default function App() { return ( <NavigationContainer> <MyDrawer /> </NavigationContainer> ); }
드로워 현재 상태 받기
드로워의 open, close 상태를 받으려면
useIsDrawerOpen()
함수를 쓰면 됨.import { useIsDrawerOpen } from '@react-navigation/drawer'; // ... const isDrawerOpen = useIsDrawerOpen();