我正在使用此代码(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} />;
}
}