在React Native中,我从一个屏幕导航到另一个屏幕。我在两个屏幕上的componentWillMount方法上加载订单。当我导航到第二个屏幕时,第一个被卸载,但是第二个也被自动卸载并返回第一个屏幕。我不知道为什么。
Navigatort.tsx
const MenuDrawerNavigator = createDrawerNavigator(
{
Orders: {
screen: DriverOrdersScreenContainer,
},
CurrentRoute: {
screen: CurrentRouteScreenContainer,
},
},
{
initialRouteName: 'CurrentRoute',
contentComponent: SideMenuContainer,
unmountInactiveRoutes: true,
}
);
export const AppStack = createStackNavigator({
Login: {
screen: LoginScreenContainer,
navigationOptions: {
header: null
}
},
DrawerNavigator: {
screen: MenuDrawerNavigator,
}
},
{
initialRouteName: 'Login',
defaultNavigationOptions: ({navigation}) => ({
title: "Main",
header: <MenuBarContainer navigation={navigation} />,
})
}
);
const Navigator = createAppContainer(AppStack);
export default Navigator;
我使用此方法在屏幕之间导航:
navigateToScreen = (route: string) => () => {
const {navigation} = this.props;
const navigateAction = NavigationActions.navigate({
routeName: route
});
navigation.dispatch(navigateAction);
navigation.dispatch(DrawerActions.closeDrawer());
};
第一个屏幕 方法-当我导航到第二个屏幕时,我收到了alert(2)
componentWillMount(): void {
const {getRoute} = this.props;
getRoute();
this.timer = setInterval(async () => await getRoute(), 60000);
}
componentWillUnmount() {
alert(2);
clearInterval(this.timer);
}
第二屏幕 方法-导航到此屏幕后,我也收到了alert(1),然后自动导航到“第一屏幕”。
componentWillMount(): void {
const {getOrders} = this.props;
getOrders();
this.timer = setInterval(async () => await getOrders(), 60000);
}
componentWillUnmount() {
alert(1);
clearInterval(this.timer);
}