我正在使用react native导航(react- navigation)StackNavigator。它从应用程序整个生命周期的“登录”页面开始。我不想使用返回选项,返回“登录”屏幕。有谁知道登录屏幕后如何将其隐藏在屏幕上?顺便说一句,我也使用以下命令将其隐藏在登录屏幕中:
const MainStack = StackNavigator({ Login: { screen: Login, navigationOptions: { title: "Login", header: { visible: false, }, }, }, // ... other screens here })
1)要使后退按钮在react-navigation v2或更高版本中消失:
navigationOptions: { title: 'MyScreen', headerLeft: null }
2)如果您要清理导航堆栈:
假设您位于要从其导航的屏幕上:
如果您使用的是反应导航版本v5或更高版本 ,则可以使用navigation.reset或CommonActions.reset:
navigation.reset
CommonActions.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' }, }, ], }) );
v2-v4 使用StackActions.reset(...)
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
NavigationActions.reset
3)对于Android,您还必须使用BackHandler禁用硬件后退按钮 :
http://reactnative.dev/docs/backhandler.html
或者如果您想使用钩子:
https://github.com/react-native- community/hooks#usebackhandler
否则,如果导航堆栈为空,则应用将在按android硬件后退按钮时关闭。