简体   繁体   English

React Native - 如何使用抽屉导航管理本机后退按钮

[英]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 我按照以下代码重新创建了抽屉导航: 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 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 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 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" news.js“一页示例”

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? 现在,如何仅在“News.js”页面中插入标题中的后退按钮而不是经典菜单(DrawerStack)?

In Android you have to handle back button actions by yourself with BackHandler from react-native. 在Android中,您必须使用来自react-native的BackHandler自行处理后退按钮操作。

First of all 首先

import { BackHandler } from 'react-native';

in ComponentDidMount add an event listener to listen for backpress: 在ComponentDidMount中添加一个事件监听器来监听backpress:

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

in ComponentwillUnmount make sure you remove the listener: 在ComponentwillUnmount中确保删除监听器:

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 也可以访问此链接: 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. 如果您想使用后退按钮返回上一个屏幕抽屉导航不适合您,您应该尝试使用Stack Navigator。

You need create the button in your news screen too, like this. 您也需要在新闻屏幕中创建按钮,就像这样。

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

export default class News extends React.Component {

    static navigationOptions = ({navigation}) => {
        return {
            headerLeft: --- PUT HERE YOU CUSTOM BUTTON (Use navigation.goBack() in onPress)
        }
    }

    render() {

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

To make better, you can create a new screen with only your custom navigation options. 为了做得更好,您可以创建仅包含自定义导航选项的新屏幕。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM