小编典典

在反应导航中禁用后退按钮

reactjs

我正在使用react native导航(react-
navigation)StackNavigator。它从应用程序整个生命周期的“登录”页面开始。我不想使用返回选项,返回“登录”屏幕。有谁知道登录屏幕后如何将其隐藏在屏幕上?顺便说一句,我也使用以下命令将其隐藏在登录屏幕中:

const MainStack = StackNavigator({
  Login: {
    screen: Login,
    navigationOptions: {
      title: "Login",
      header: {
        visible: false,
      },
    },
  },
  // ... other screens here
})

阅读 251

收藏
2020-07-22

共1个答案

小编典典

1)要使后退按钮在react-navigation v2或更高版本中消失:

navigationOptions:  {
    title: 'MyScreen',
    headerLeft: null
}

2)如果您要清理导航堆栈:

假设您位于要从其导航的屏幕上:

如果您使用的是反应导航版本v5或更高版本 ,则可以使用navigation.resetCommonActions.reset

 // Replace current navigation state with a new one,
 // index value will be the current active route:

navigation.reset({
  index: 0,
  routes: [{ name: 'Profile' }],
});

来源和更多信息在这里:https :
//reactnavigation.org/docs/navigation-
prop/#reset

要么:

navigation.dispatch(
  CommonActions.reset({
    index: 1,
    routes: [
      { name: 'Home' },
      {
        name: 'Profile',
        params: { user: 'jane' },
      },
    ],
  })
);
来源和更多信息在这里:https
//reactnavigation.org/docs/navigation-
actions/#reset

对于旧版本的反应导航:

v2-v4 使用StackActions.reset(...)

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

const resetAction = StackActions.reset({
  index: 0, // <-- currect active route from actions array
  actions: [
    NavigationActions.navigate({ routeName: 'myRouteWithDisabledBackFunctionality' }),
  ],
});

this.props.navigation.dispatch(resetAction);

v1 使用NavigationActions.reset

3)对于Android,您还必须使用BackHandler禁用硬件后退按钮

http://reactnative.dev/docs/backhandler.html

或者如果您想使用钩子:

https://github.com/react-native-
community/hooks#usebackhandler

否则,如果导航堆栈为空,则应用将在按android硬件后退按钮时关闭。

2020-07-22