【问题标题】:Remove screen from stack navigator从堆栈导航器中删除屏幕
【发布时间】:2018-06-09 19:58:12
【问题描述】:

我观察到后退按钮逻辑可以查看堆栈中的屏幕序列。我在堆栈导航器中放置了一个抽屉导航器。

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

进入应用程序后如何从堆栈中删除启动画面,所以当我按下返回按钮仪表板时,它将退出应用程序而不是进入启动画面。

/* @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>;

【问题讨论】:

    标签: react-native react-navigation


    【解决方案1】:

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

    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

    【讨论】:

    • 但这样做不会从堆栈中删除启动画面。我想在仪表板上按后退出应用程序,而不是到导航到其他屏幕的启动屏幕。
    • 此代码将清除所有历史记录,因此启动画面不再是堆栈的一部分
    • 您提供的链接无效,能否请您提供文档的固定链接
    • 以上链接对版本 3 无效,请使用此解决方案stackoverflow.com/a/50781113/404339
    • @EhsanZargarErshadi 更新了我对较新版本的 react-navigation 的答案。看起来自去年以来有些事情发生了变化。所以我的回答仍然有效
    【解决方案2】:

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

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

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

    【讨论】:

      【解决方案3】:

      我用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

      【讨论】:

        【解决方案4】:

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

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

        replace 动作允许替换处于导航状态的路线。您可以将其导入为:-

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

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

        【讨论】:

        • 优雅的解决方案!
        【解决方案5】:
        useEffect(() => {
        setTimeout(() => {
          navigation.reset({
            index:0,
            routes:[
              {
                name:"Welcome",
              //  params:{key:'param'},
              },
            ],
          });
        }, 1000);},[]);
        

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

        onPress={()=>navigation.reset({
            index:0,
            routes:[
              {
                name:"NameOfScreen",
              //  params:{key:'param'},
              },
            ],
          })}
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2021-07-11
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2022-01-20
          • 2022-01-22
          • 2020-12-07
          相关资源
          最近更新 更多