Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multiple Instances opened when component is loaded #9

Open
KimeraMoses opened this issue Mar 20, 2023 · 4 comments
Open

Multiple Instances opened when component is loaded #9

KimeraMoses opened this issue Mar 20, 2023 · 4 comments

Comments

@KimeraMoses
Copy link

image

This happens randomly and I can't figure out what's causing it.

@AnaGetovska
Copy link

I have the same issue. When I refresh the page I get the same problem.

@t-ega
Copy link

t-ega commented Mar 27, 2023

i have the same issue too!!. At first i thought it was a bug from my end.

@myromr
Copy link

myromr commented Mar 28, 2023

In React, there is a thing called StrictMode. React StrictMode renders components twice in the development environment but not in production. This is to detect any problems with your code. useEffect itself will only be called once. StrictMode is automatically enabled in development mode, but you can disable it by removing the <React.StrictMode> wrapper from your index.js or App.js file or by commenting it out.

Source: https://youtu.be/k1Zyj4Qw56M

StrictMode

I hope this helps!

@dovh-me
Copy link

dovh-me commented Feb 25, 2024

I was able to overcome this issue from happening without disabling react strict mode by using the useRef hook.

Replace your HTML5QrcodePlugin component with the below code

const Html5QrcodePlugin = (props: any) => {
  const regionRef = useRef(null);
  const scannerRef = useRef<Html5QrcodeScanner | null>(null);

  useEffect(() => {
    if (!scannerRef.current) {
      // when component mounts
      const config = createConfig(props);
      const verbose = props.verbose === true;
      // Suceess callback is required.
      if (!props.qrCodeSuccessCallback) {
        throw 'qrCodeSuccessCallback is required callback.';
      }
      const html5QrcodeScanner = new Html5QrcodeScanner(qrcodeRegionId, config, verbose);
      scannerRef.current = html5QrcodeScanner;
    }

    const scanner = scannerRef.current;
    scanner.render(props.qrCodeSuccessCallback, props.qrCodeErrorCallback);

    // cleanup function when component will unmount
    return () => {
      scanner.clear().catch((error) => {
        console.error('Failed to clear html5QrcodeScanner. ', error);
      });
    };
  }, []);

  return <div ref={regionRef} id={qrcodeRegionId} />;
};

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants