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

Help with code - Show Camera - Read code - Close camera and save value #32

Open
mDeSimone-DFactorySrl opened this issue Jun 28, 2024 · 0 comments

Comments

@mDeSimone-DFactorySrl
Copy link

Hi all,
i am trying to make a simple app that, when you press a button will show the camera, read a Code and then show the result.
My code is:

import { useState } from 'react';
import { useZxing } from "react-zxing";

const NewRecords = () => {
    const [result, setResult] = useState("");
    const [isSectionVisible, setIsSectionVisible] = useState(false); // Initialize visibility state
  const { ref } = useZxing({
    onDecodeResult(result) {
        console.log(result.getText());
      setResult(result.getText());
    },
  });

  return (
    <>
    <button onClick={() => setIsSectionVisible(!isSectionVisible)}>
        {isSectionVisible ? "Hide" : "Show"} camera
      </button>
      {isSectionVisible && (
        <>
          <video ref={ref} />
          <p>
            <span>Last result:</span>
            <span>{result}</span>
          </p>
        </>
      )}
    </>
  );
};

export default NewRecords;

With this when i press the "Show button" the camera wont work and is it not asking for permission.
If i start my code with "isSectionVisible" at true the camera is working but when i close it and reopen it will stop working.
In this second case if a code is readed this onDecodeResult(result) { console.log(result.getText()); setResult(result.getText()); }, console.log will spam continusly the result.

Can someone help me with my code?
Sorry for bad eng

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

1 participant