I am new to react native and I have react native app for android and I am using Navigation 5 for navigating to the next page. The application workflow is loginScreen -> HomeScreen -> ExistingStockScreen -> ScanSkidBarcodeScreen -> ScanItemsScreen. I am handling backbutton press with this code on ScanItemScreen.
navigation.addListener('beforeRemove', (e) => {
// Prevent default behavior of leaving the screen
e.preventDefault();
if (this.state.items[0].barCode !== "" && this.state.items[5].barCode !== "") {
Alert.alert(
'Error!',
'You can only scan six items at a time.',
[
{ text: "OK", style: 'cancel', onPress: () => { } },
]
);
}
else if (this.state.items[0].barCode !== "") {
Alert.alert(
'Attention!',
'You have unsaved items. Please click on UPDATE STATUS first and go back.',
[{ text: "OK", style: 'cancel', onPress: () => { } },
{
text: 'Go Back',
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),
},]
);
} else {
navigation.dispatch(e.data.action)
}
})
}
However navigation.dispatch(e.data.action) code is going to back screen but it logs out the user for some reason. So I am thinking to use navigation.goBack() instead. What's the difference between navigation.goBack() and navigation.dispatch(e.data.action) ?