Skip to content

Commit

Permalink
beforeRemove listener cleanup (#358)
Browse files Browse the repository at this point in the history
  • Loading branch information
arekkubaczkowski authored and jdivock-stripe committed Aug 31, 2022
1 parent 4cffee7 commit 25564e5
Showing 1 changed file with 13 additions and 3 deletions.
16 changes: 13 additions & 3 deletions dev-app/src/screens/DiscoverReadersScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ export default function DiscoverReadersScreen() {
const handleGoBack = useCallback(
async (action: NavigationAction) => {
await cancelDiscovering();

if (navigation.canGoBack()) {
navigation.dispatch(action);
}
Expand All @@ -118,14 +119,23 @@ export default function DiscoverReadersScreen() {
headerBackTitle: 'Cancel',
});

navigation.addListener('beforeRemove', (e) => {
if (!discoveringLoading) {
const listener = navigation.addListener('beforeRemove', (e) => {
if (!discoveringLoading || !!connectingReader) {
return;
}

e.preventDefault();
handleGoBack(e.data.action);
});
}, [navigation, cancelDiscovering, discoveringLoading, handleGoBack]);

return () => navigation.removeListener('beforeRemove', listener);
}, [
navigation,
cancelDiscovering,
discoveringLoading,
handleGoBack,
connectingReader,
]);

const handleDiscoverReaders = useCallback(async () => {
setDiscoveringLoading(true);
Expand Down

0 comments on commit 25564e5

Please sign in to comment.