3

I'm building an app where I have deep links to open app and run a specific function based on the url. But the event triggers multiple times and run same function after that I have 10+ alerts which needs to be confirmed.

This is from running app on real device with livereload

Here is my code as React Component

import { useEffect } from "react";
import { App, URLOpenListenerEvent } from "@capacitor/app";

import { useCamera } from "src/hooks";

const AppUrlListener: React.FC<any> = () => {
  const { startActionApprovalProcess } = useCamera();

  useEffect(() => {
    App.addListener("appUrlOpen", (event: URLOpenListenerEvent) => {
      let actionUrl: string | null = event.url.replace("myrecheck", "https");
      startActionApprovalProcess(actionUrl);
      actionUrl = null;
    });
    App.addListener("appStateChange", event => {
      console.log("appStateChange:", JSON.stringify(event));
    });
  }, [startActionApprovalProcess]);

  return null;
};

export default AppUrlListener;

I'm importing it on App.tsx (Firebase Notifications Listener too which runs multiple times again) like a normal component

<AppUrlListener />
<NotificationsListener />

And here are my console logs First I'm moving from app to the browser state changes Then open app from deep link and runs the event from which comes origin logs Then comes apps new state And then again logs from my function

VM3:207 result App.addListener (#8693379)
index.tsx:16 appStateChange: {"isActive":false}
VM3:207 result App.addListener (#8693383)
index.tsx:16 appStateChange: {"isActive":false}
VM3:207 result App.addListener (#8693385)
index.tsx:16 appStateChange: {"isActive":false}
VM3:207 result App.addListener (#8693387)
index.tsx:16 appStateChange: {"isActive":false}
VM3:207 result App.addListener (#8693389)
index.tsx:16 appStateChange: {"isActive":false}
VM3:207 result App.addListener (#8693391)
index.tsx:16 appStateChange: {"isActive":false}
VM3:207 result App.addListener (#8693393)
index.tsx:16 appStateChange: {"isActive":false}
VM3:207 result App.addListener (#8693395)
index.tsx:16 appStateChange: {"isActive":false}
VM3:207 result App.addListener (#8693402)
index.tsx:16 appStateChange: {"isActive":false}
VM3:207 result App.addListener (#8693404)
index.tsx:16 appStateChange: {"isActive":false}
VM3:207 result App.addListener (#8693378)
qr-decode.ts:12 url origin https://my.recheck.io
VM3:207 result App.addListener (#8693382)
qr-decode.ts:12 url origin https://my.recheck.io
VM3:207 result App.addListener (#8693384)
qr-decode.ts:12 url origin https://my.recheck.io
VM3:207 result App.addListener (#8693386)
qr-decode.ts:12 url origin https://my.recheck.io
VM3:207 result App.addListener (#8693388)
qr-decode.ts:12 url origin https://my.recheck.io
VM3:207 result App.addListener (#8693390)
qr-decode.ts:12 url origin https://my.recheck.io
VM3:207 result App.addListener (#8693392)
qr-decode.ts:12 url origin https://my.recheck.io
VM3:207 result App.addListener (#8693394)
qr-decode.ts:12 url origin https://my.recheck.io
VM3:207 result App.addListener (#8693401)
qr-decode.ts:12 url origin https://my.recheck.io
VM3:207 result App.addListener (#8693403)
qr-decode.ts:12 url origin https://my.recheck.io
VM3:207 result App.addListener (#8693379)
index.tsx:16 appStateChange: {"isActive":true}
VM3:207 result App.addListener (#8693383)
index.tsx:16 appStateChange: {"isActive":true}
VM3:207 result App.addListener (#8693385)
index.tsx:16 appStateChange: {"isActive":true}
VM3:207 result App.addListener (#8693387)
index.tsx:16 appStateChange: {"isActive":true}
VM3:207 result App.addListener (#8693389)
index.tsx:16 appStateChange: {"isActive":true}
VM3:207 result App.addListener (#8693391)
index.tsx:16 appStateChange: {"isActive":true}
VM3:207 result App.addListener (#8693393)
index.tsx:16 appStateChange: {"isActive":true}
VM3:207 result App.addListener (#8693395)
index.tsx:16 appStateChange: {"isActive":true}
VM3:207 result App.addListener (#8693402)
index.tsx:16 appStateChange: {"isActive":true}
VM3:207 result App.addListener (#8693404)
index.tsx:16 appStateChange: {"isActive":true}
qr-decode.ts:17 Success new apiUrl:  https://my.recheck.io
VM3:233 native App.getInfo (#8693412)
qr-decode.ts:17 Success new apiUrl:  https://my.recheck.io
VM3:233 native App.getInfo (#8693413)
qr-decode.ts:17 Success new apiUrl:  https://my.recheck.io
VM3:233 native App.getInfo (#8693414)
qr-decode.ts:17 Success new apiUrl:  https://my.recheck.io
VM3:233 native App.getInfo (#8693415)
qr-decode.ts:17 Success new apiUrl:  https://my.recheck.io
VM3:233 native App.getInfo (#8693416)
qr-decode.ts:17 Success new apiUrl:  https://my.recheck.io
VM3:233 native App.getInfo (#8693417)
qr-decode.ts:17 Success new apiUrl:  https://my.recheck.io
VM3:233 native App.getInfo (#8693418)
qr-decode.ts:17 Success new apiUrl:  https://my.recheck.io
VM3:233 native App.getInfo (#8693419)
qr-decode.ts:17 Success new apiUrl:  https://my.recheck.io
VM3:233 native App.getInfo (#8693420)
qr-decode.ts:17 Success new apiUrl:  https://my.recheck.io
VM3:233 native App.getInfo (#8693421)
VM3:207 result App.getInfo (#8693412)
VM3:207 result App.getInfo (#8693413)
VM3:207 result App.getInfo (#8693414)
VM3:207 result App.getInfo (#8693415)
VM3:207 result App.getInfo (#8693416)
VM3:207 result App.getInfo (#8693417)
VM3:207 result App.getInfo (#8693418)
VM3:207 result App.getInfo (#8693419)
VM3:207 result App.getInfo (#8693420)
VM3:207 result App.getInfo (#8693421)
Ionic:

   Ionic CLI       : 6.17.0 (/Users/byurhanbeyzat/.nvm/versions/node/v16.3.0/lib/node_modules/@ionic/cli)
   Ionic Framework : @ionic/react 5.7.0

Capacitor:

   Capacitor CLI      : 3.1.2
   @capacitor/android : 3.2.2
   @capacitor/core    : 3.2.2
   @capacitor/ios     : 3.2.2

Utility:

   cordova-res                          : not installed globally
   native-run (update available: 1.4.1) : 1.4.0

System:

   NodeJS : v16.3.0 (/Users/byurhanbeyzat/.nvm/versions/node/v16.3.0/bin/node)
   npm    : 7.21.1
   OS     : macOS Big Sur
dom.macs
  • 776
  • 7
  • 15

1 Answers1

1

Late answer, but for people arriving here from Google:

This useEffect is probably running multiple times. Adding the same listener 10+ times to the same event.

You'll need a Cleanup function to remove the duplicate listeners

useEffect(() => {
   // add listeners are here

   // Below function will be executed similar to the old `componentWillUnmount` function on classes
   return () => {
       App.removeAllListeners();
   }
});
Tim
  • 11
  • 2