Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React Native - How to manage the native back button with Drawer Navigation

I recreated the Drawer Navigation following this code: https://github.com/mariodev12/react-native-menu-drawer-navigator

Everything works correctly but now I do not know how to handle the native button to go back .. I would like to always return to the previous page, but if you press twice in the home exit the app.

This is my Code:

App.js

import React from 'react';
import {StackNavigator} from 'react-navigation';
import DrawerStack from './src/stacks/drawerStack';

const Navigator = StackNavigator({
    drawerStack: {screen: DrawerStack}
}, {
    headerMode: 'none',
    initialRouteName: 'drawerStack'
})

export default Navigator

drawerStack.js

import React from 'react'
import {StackNavigator, DrawerActions} from "react-navigation";
import {Text, View, TouchableOpacity} from 'react-native';
import Home from "../components/home";
import DrawerScreen from "./drawerScreen";

const DrawerNavigation = StackNavigator({
    DrawerStack: {screen: DrawerScreen}
}, {
    headerMode: 'float',
    navigationOptions: ({navigation}) => ({
        headerStyle: {
            backgroundColor: 'rgb(255,45,85)',
            paddingLeft: 10,
            paddingRight: 10
        },
        title: 'Home',
        headerTintColor: 'white',
        headerLeft: <View>
            <TouchableOpacity
                onPress={() => {
                    if (navigation.state.isDrawerOpen === false) {
                        navigation.dispatch(DrawerActions.openDrawer());
                    } else {
                        navigation.dispatch(DrawerActions.closeDrawer());
                    }
                }}>
                <Text>Menu</Text>
            </TouchableOpacity>
        </View>
    })
})

export default DrawerNavigation;

drawerScreen.js

import {DrawerNavigator} from 'react-navigation'
import Home from '../components/home';
import Login from '../components/login';
import Contacts from '../components/contacts';
import News from '../components/news';

const DrawerScreen = DrawerNavigator({
    Home: {screen: Home},
    Login: {screen: Login},
    Contacts: {screen: Contacts},
    News: {screen: News}
}, {
    headerMode: 'none',
    initialRouteName: 'Home'
})

export default DrawerScreen;

news.js "Example of one page"

import React from "react";
import {Text, View} from 'react-native';

export default class News extends React.Component {
    render() {

        return (
            <View>
                <Text> Here Leave the News!! </Text>
            </View>
        );
    }
}

Now, how do I insert the back button in the header instead of the classic menu (DrawerStack) for only the 'News.js' page?

like image 480
Nobady Avatar asked Oct 15 '25 14:10

Nobady


1 Answers

In Android you have to handle back button actions by yourself with BackHandler from react-native.

First of all

import { BackHandler } from 'react-native';

in ComponentDidMount add an event listener to listen for backpress:

componentDidMount() {
    BackHandler.addEventListener("hardwareBackPress", this.onBackPress);
}

in ComponentwillUnmount make sure you remove the listener:

componentWillUnmount() {
    BackHandler.removeEventListener("hardwareBackPress", this.onBackPress);
}

then

 onBackPress = () => {

     //inside here do what you want with single back button
 }

Checkout this link too: https://reactnavigation.org/docs/en/drawer-based-navigation.html

If you want to go back to previous Screen with back button drawer navigation isn't for you and you should try to use Stack Navigator.

like image 73
Giorgos Kartalis Avatar answered Oct 17 '25 03:10

Giorgos Kartalis