ThinkAndCode
ThinkAndCode

Reputation: 1657

How To close the navigation drawer with hardware(android) back button?

I am using react-native-drawer-layout for navigation drawer.

<DrawerLayoutAndroid
    drawerWidth={100}
    ref={'Drawer'}
    drawerPosition={DrawerLayoutAndroid.positions.Right}
    renderNavigationView={() => NavigationView}
/>

When i try to close the drawer total app is getting closed.

Could anybody let me know how to close the drawer with hardware(android) button?

Upvotes: 1

Views: 895

Answers (1)

Convict Moody
Convict Moody

Reputation: 833

I never user DrawerLayoutAndroid but according to the React Native Docs it has method "closeDrawer()" that is supposed to close it... by using BackHandler you should add an event listener to the component containing the drawer layout, add this:

componentDidMount(){
    BackHandler.addEventListener('hardwareBackPress', ()=>{
        this.refs.Drawer.closerDrawer();
        return true;
        });
}

Upvotes: 1

Related Questions