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

Two cameras appear if reloading implement with next js #916

Open
paresiqbal opened this issue Apr 25, 2024 · 1 comment
Open

Two cameras appear if reloading implement with next js #916

paresiqbal opened this issue Apr 25, 2024 · 1 comment

Comments

@paresiqbal
Copy link

Describe the Question
On first load of web it's only shows one camera, as intended. but when reloading it will shows two cameras, even when reload on other page. I have to restart the web to make the camera normal again
On every following load two cameras appear (see attachment)

Context about your web application
Windows 11
happens in Chrome, Firefox and Edge
"next": "^14.1.1",
"react": "^18",

image

  const { presensiData } = usePresensi();

  const qrRef = useRef<HTMLDivElement>(null);
  const [qrData, setQrData] = useState<QRData | null>(null);
  const [apiResponse, setApiResponse] = useState<ApiResponse | null>(null);

  const onScanSuccess = useCallback(
    (decodedText: string, decodedResult: any) => {
      console.log(`Code matched = ${decodedText}`, decodedResult);
      parseQRData(decodedText);
    },
    []
  );

  useEffect(() => {
    let scanner: Html5QrcodeScanner | null = null;

    if (qrRef.current) {
      scanner = new Html5QrcodeScanner(
        qrRef.current.id,
        { fps: 10, qrbox: { width: 250, height: 250 } },
        false
      );
      scanner.render(onScanSuccess, onScanFailure);
    }

    return () => {
      scanner?.clear();
    };
  }, [onScanSuccess]);
     <div className="flex flex-col justify-center items-center">
        <div id="reader" ref={qrRef} style={{ width: "350px" }}></div>
        {apiResponse && (
          <div>
            <p>
              Status:
              {apiResponse.message || apiResponse.error || "Error"}
            </p>
          </div>
        )}
      </div>

Additional context
Add any other context about the problem here.

@ratnakarm
Copy link

ratnakarm commented Apr 30, 2024

This is because of Strict Mode
Strict Mode enables the following checks in development:
Your components will re-render an extra time to find bugs caused by impure rendering.
Your components will re-run Effects an extra time to find bugs caused by missing Effect cleanup.
Your components will be checked for usage of deprecated APIs.
All of these checks are development-only and do not impact the production build.

you can remove strict mode in index.js

root.render(
    <App />
)

About strictmode in react

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

No branches or pull requests

2 participants