【发布时间】:2021-05-03 07:44:50
【问题描述】:
我试图在用户单击返回按钮时发出警报,在该按钮将提供两个选项,是或否。如果用户单击“否”,用户将停留在该屏幕上,如果用户按下“是” " 那么,我想显示一些不同的屏幕。
基本上,我想阻止用户返回上一个屏幕,而是将用户重定向到另一个屏幕。 这是我试图使其工作的示例 useEffect 代码:
useEffect(() => {
navigation.addListener('beforeRemove', (e) => {
e.preventDefault();
Alert.alert(
'Registration Process',
'Are you sure you want to cancel the registration process?',
[
{
text: 'Yes',
style: 'destructive',
// If the user confirmed, then we dispatch the action we blocked earlier
// This will continue the action that had triggered the removal of the screen
onPress: () => {
// navigation.dispatch(e.data.action);
navigation.navigate('SignUp'); // On pressing Yes, user should be shown this screen.
},
},
{text: 'No', style: 'cancel', onPress: () => {}},
],
);
});
}, [navigation]);
运行应用程序后,当我按“是”时,我会一次又一次地收到警告框。
【问题讨论】:
-
此方法将继续无限运行..检查我的答案以获得更好的实施
标签: react-native navigation screen event-listener react-native-navigation