在特定屏幕中禁用后退按钮(React Navigation Stack)
Disable back button in a specific Screen (React Navigation Stack)
您好!
我需要禁用导航栏上的后退按钮。请帮帮我。
路线
- 首页:我不想离开申请
- 成功:我不想返回结帐。
Example: click here
import React from 'react';
import { createStackNavigator } from '@react-navigation/stack';
import { OrderProvider } from '../contexts/order';
import Home from '../pages/Home';
import Checkout from '../pages/Checkout';
import Success from '../pages/Checkout/success';
const AppStack = createStackNavigator();
const AppRoutes = () => (
<OrderProvider>
<AppStack.Navigator screenOptions={{ headerShown: false }}>
<AppStack.Screen name="Home" component={Home} /> <-- here
<AppStack.Screen name="Checkout" component={Checkout} />
<AppStack.Screen name="Success" component={Success} /> <--- here
</AppStack.Navigator>
</OrderProvider>
);
export default AppRoutes;
import React from 'react';
import { View} from 'react-native';
const Success = () => {
return (
<View />
);
};
export default Success;
您可以执行以下操作:
const Home = () => {
useFocusEffect(
React.useCallback(() => {
const onBackPress = () => {
return true;
};
BackHandler.addEventListener('hardwareBackPress', onBackPress);
return () =>
BackHandler.removeEventListener('hardwareBackPress', onBackPress);
}, []),
);
// ...
};
const Success = ({navigation}) => {
useFocusEffect(
React.useCallback(() => {
const onBackPress = () => {
navigation.navigate('Home');
return true;
};
BackHandler.addEventListener('hardwareBackPress', onBackPress);
return () =>
BackHandler.removeEventListener('hardwareBackPress', onBackPress);
}, []),
);
// ...
};
我已将其设置为在调用 hardwareBackPress
事件时通过 returning true
单击 Home
屏幕不会执行任何操作。
对于 Success
屏幕,我导航回 Home
。
我认为这就是您在阅读您的问题时所寻找的行为。
请务必不要忘记从 useFocusEffect
导入
在你使用它的任何地方反应导航:
import { useFocusEffect } from '@react-navigation/native';
什么时候returntrue
或false
在hardwareBackPress
事件处理函数中在react导航文档中解释:
Returning true from onBackPress denotes that we have handled the
event, and react-navigation's listener will not get called, thus not
popping the screen. Returning false will cause the event to bubble up
and react-navigation's listener will pop the screen.
如果您想了解更多信息,请阅读此处的文档:https://reactnavigation.org/docs/custom-android-back-button-handling/。
您好!
我需要禁用导航栏上的后退按钮。请帮帮我。
路线
- 首页:我不想离开申请
- 成功:我不想返回结帐。
Example: click here
import React from 'react';
import { createStackNavigator } from '@react-navigation/stack';
import { OrderProvider } from '../contexts/order';
import Home from '../pages/Home';
import Checkout from '../pages/Checkout';
import Success from '../pages/Checkout/success';
const AppStack = createStackNavigator();
const AppRoutes = () => (
<OrderProvider>
<AppStack.Navigator screenOptions={{ headerShown: false }}>
<AppStack.Screen name="Home" component={Home} /> <-- here
<AppStack.Screen name="Checkout" component={Checkout} />
<AppStack.Screen name="Success" component={Success} /> <--- here
</AppStack.Navigator>
</OrderProvider>
);
export default AppRoutes;
import React from 'react';
import { View} from 'react-native';
const Success = () => {
return (
<View />
);
};
export default Success;
您可以执行以下操作:
const Home = () => {
useFocusEffect(
React.useCallback(() => {
const onBackPress = () => {
return true;
};
BackHandler.addEventListener('hardwareBackPress', onBackPress);
return () =>
BackHandler.removeEventListener('hardwareBackPress', onBackPress);
}, []),
);
// ...
};
const Success = ({navigation}) => {
useFocusEffect(
React.useCallback(() => {
const onBackPress = () => {
navigation.navigate('Home');
return true;
};
BackHandler.addEventListener('hardwareBackPress', onBackPress);
return () =>
BackHandler.removeEventListener('hardwareBackPress', onBackPress);
}, []),
);
// ...
};
我已将其设置为在调用 hardwareBackPress
事件时通过 returning true
单击 Home
屏幕不会执行任何操作。
对于 Success
屏幕,我导航回 Home
。
我认为这就是您在阅读您的问题时所寻找的行为。
请务必不要忘记从 useFocusEffect
导入
在你使用它的任何地方反应导航:
import { useFocusEffect } from '@react-navigation/native';
什么时候returntrue
或false
在hardwareBackPress
事件处理函数中在react导航文档中解释:
Returning true from onBackPress denotes that we have handled the event, and react-navigation's listener will not get called, thus not popping the screen. Returning false will cause the event to bubble up and react-navigation's listener will pop the screen.
如果您想了解更多信息,请阅读此处的文档:https://reactnavigation.org/docs/custom-android-back-button-handling/。