返回到Android中处理硬件后退按钮的上一个路由

时间:2018-05-01 07:57:33

标签: reactjs react-native react-navigation

我正在使用此代码(here)来处理Android中的硬件后退按钮。

我已嵌套路线,当您点击后退按钮时,在此代码中,转到initialRoute

我的路线如下:Home => PoolList => PoolDetails => Cart

Cart路线中,点击后退按钮,返回Home。我想回来如下:

Home <= PoolList <= PoolDetails <= Cart

如何更改此代码?

import React from "react";
import { BackHandler } from "react-native";
import { addNavigationHelpers, NavigationActions, DrawerNavigator } from "react-navigation";

const AppNavigation = DrawerNavigator(
  {
    Home: { screen: HomeScreen },
    PoolList: { screen: PoolListScreen },
    PoolDetails: { screen: PoolDetailsScreen },
    Cart: { screen: CartScreen },
  },
  {
     contentComponent: props => <SideBar {...props} />,
     drawerPosition: 'right'
 }
);

class ReduxNavigation extends React.Component {
  componentDidMount() {
    BackHandler.addEventListener("hardwareBackPress", this.onBackPress);
  }
  componentWillUnmount() {
    BackHandler.removeEventListener("hardwareBackPress", this.onBackPress);
  }
  onBackPress = () => {
    const { dispatch, nav } = this.props;
    if (nav.index === 0) {
      return false;
    }
    dispatch(NavigationActions.back());
    return true;
  };

  render() {
    const { dispatch, nav } = this.props;
    const navigation = addNavigationHelpers({
      dispatch,
      state: nav,
      addListener,
    });

    return <AppNavigation navigation={navigation} />;
  }
}

0 个答案:

没有答案