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

时间:2018-06-07 08:58:54

标签: android react-native navigation-drawer react-navigation

我按照以下代码重新创建了抽屉导航:https://github.com/mariodev12/react-native-menu-drawer-navigator

一切正常但现在我不知道如何处理本机按钮返回..我想总是返回上一页,但如果你在主页退出应用程序按两次。

这是我的代码:

  

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“一页示例”

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>
        );
    }
}

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

2 个答案:

答案 0 :(得分:2)

在Android中,您必须使用来自react-native的BackHandler自行处理后退按钮操作。

首先

import { BackHandler } from 'react-native';
ComponentDidMount中的

添加一个事件监听器来监听backpress:

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

确保删除监听器:

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

然后

 onBackPress = () => {

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

也查看此链接: https://reactnavigation.org/docs/en/drawer-based-navigation.html

如果您想使用后退按钮返回上一个屏幕抽屉导航不适合您,您应该尝试使用Stack Navigator。

答案 1 :(得分:1)

您也需要在新闻屏幕中创建按钮,就像这样。

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>
        );
    }
}

为了做得更好,您可以使用自定义导航选项创建新屏幕。