You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Multiple Instances opened when component is loaded #9
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.
Reacted by Kaloyan Kosev, mochfathurrozi, AnaGetovska, TuanLe95, Harshit Mehrotra, bradmkjr, Seyed Sina Pezeshk, Kevin Parra, Mudassar H. and Marcus Paulo Oliveira
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
constHtml5QrcodePlugin=(props: any)=>{constregionRef=useRef(null);constscannerRef=useRef<Html5QrcodeScanner|null>(null);useEffect(()=>{if(!scannerRef.current){// when component mountsconstconfig=createConfig(props);constverbose=props.verbose===true;// Suceess callback is required.if(!props.qrCodeSuccessCallback){throw'qrCodeSuccessCallback is required callback.';}consthtml5QrcodeScanner=newHtml5QrcodeScanner(qrcodeRegionId,config,verbose);scannerRef.current=html5QrcodeScanner;}constscanner=scannerRef.current;scanner.render(props.qrCodeSuccessCallback,props.qrCodeErrorCallback);// cleanup function when component will unmountreturn()=>{scanner.clear().catch((error)=>{console.error('Failed to clear html5QrcodeScanner. ',error);});};},[]);return<divref={regionRef}id={qrcodeRegionId}/>;};
This happens randomly and I can't figure out what's causing it.