从堆栈导航器中删除屏幕

时间:2017-12-29 11:00:39

标签: react-native react-navigation

我观察到Back按钮逻辑可以看到堆栈中的屏幕顺序。我有一个放置在堆栈导航器中的抽屉导航器。

在堆栈顶部我有Splash画面。在仪表板上,当我按下后退按钮时,我需要启动屏幕。

如何在进入应用程序后从堆栈中删除启动画面,因此当我按下后退按钮Dashboard时,它将退出应用程序,而不是进入SPLASH SCREEN。

/* @flow */

import React from "react";
import { Platform, Text } from "react-native";
import { Root } from "native-base";
import { StackNavigator, DrawerNavigator} from "react-navigation";
import Register from "./components/Register";
import Available from "./components/Available";
import Splash from "./components/splash/“;
import SideBar from "./components/sidebar";
import Discover from "./components/Discover/";
import Dashboard from "./components/Dashboard/";
import Contact from "./components/Contact"



const Drawer = DrawerNavigator(
  {
    Dashboard: { screen: Dashboard },
    Discover: { screen: Discover },
    Contact: { screen: Contact},
      },
  {
    navigationOptions: {
      gesturesEnabled: false,
    },
   initialRouteName: "Dashboard",
    contentOptions: {
      activeTintColor: "#e91e63"
    },
    drawerPosition: 'right',
    contentComponent: props => <SideBar {...props} />
  }
);


const AppNavigator = StackNavigator(
    {
      Splash: { screen: Splash },
      Drawer: { screen: Drawer },                           
      Available: { screen: Available },
        Register: { screen: Register },
    },
    {
       //  initialRouteName: “Splash”,
         headerMode: "none",
    }
);

export default () =>
    <Root>
        <AppNavigator />
    </Root>;

5 个答案:

答案 0 :(得分:9)

一种解决方案是重置启动画面组件内的堆栈,并将用户重定向到您喜欢的屏幕:

import { NavigationActions } from 'react-navigation'

const resetAction = NavigationActions.reset({
  index: 0,
  actions: [
    NavigationActions.navigate({ routeName: 'Drawer'})
  ]
})
this.props.navigation.dispatch(resetAction)

适用于较新版本的react-navigation

import { StackActions, NavigationActions } from 'react-navigation';

const resetAction = StackActions.reset({
  index: 0,
  actions: [NavigationActions.navigate({ routeName: 'Profile' })],
});
this.props.navigation.dispatch(resetAction);

Link to the official documentation

答案 1 :(得分:2)

我使用this

解决了它

代码:

const prevGetStateForActionHomeStack = HomeStack.router.getStateForAction;
HomeStack.router = {
  ...HomeStack.router,
  getStateForAction(action, state) {
    if (state && action.type === 'ReplaceCurrentScreen') {
      const routes = state.routes.slice(0, state.routes.length - 1);
      routes.push(action);
      return {
        ...state,
        routes,
        index: routes.length - 1,
      };
    }
    return prevGetStateForActionHomeStack(action, state);
  },
};
replaceScreen = () => {
  const { locations, position } = this.props.navigation.state.params;
  this.props.navigation.dispatch({
    key: 'NearMeMap',
    type: 'ReplaceCurrentScreen',
    routeName: 'NearMeMap',
    params: { locations, position },
  });
};

如果您需要深入的代码说明,请观看这​​段简短的视频here

答案 2 :(得分:0)

df = pd.DataFrame({'xvals': xvals, 'yvals': yvals}) 

你也可以像这样触发这个 onPress:

useEffect(() => {
setTimeout(() => {
  navigation.reset({
    index:0,
    routes:[
      {
        name:"Welcome",
      //  params:{key:'param'},
      },
    ],
  });
}, 1000);},[]);

答案 3 :(得分:0)

react-navigation 5.x 版本中。如果你想打开新屏幕并删除以前的堆栈屏幕,你可以写:-

navigation.dispatch(StackActions.replace('screen_name', {params: {}}));

replace 操作允许在导航状态下替换路线。您可以将其导入为:-

import { StackActions } from '@react-navigation/native';

如果您想在堆栈导航上执行其他操作,请查看此链接- https://reactnavigation.org/docs/stack-actions

答案 4 :(得分:0)

最简单的解决方案之一是,用新屏幕替换当前屏幕,这样用户就不会被允许返回。

const SplashScreen: () => {
    ...
  navigation.replace("LoginScreen"); // Move forward and Remove this screen from stack

https://reactnavigation.org/docs/stack-actions/#replace