简体   繁体   English

如何使用 React 将扫描 QR 码的数据存储在数组中?

[英]How can I store the data from scanning a QR code in an array using react?

I'm trying to store the data from a scanned QR code in a new array upon scan.我试图在扫描时将扫描的 QR 码中的数据存储在新数组中。 I would then like to display the array.然后我想显示数组。 For example, if I scan a QR code with the app, and the QR code contains a specific string as a title, I would like the string added to an array.例如,如果我用应用程序扫描 QR 码,并且 QR 码包含一个特定的字符串作为标题,我想将该字符串添加到一个数组中。 Any help would be much appreciated任何帮助将非常感激

import React, { useState } from "react";
import { Fab, TextareaAutosize } from "@material-ui/core";
import { ArrowBack } from "@material-ui/icons";
import { Link } from "react-router-dom";
import QrScan from "react-qr-reader";

const QRscanner = () => {
  const [qrscan, setQrscan] = useState("No result");
  const handleScan = (data) => {
    if (data) {
      setQrscan(data);
    }
  };
  const handleError = (err) => {
    console.error(err);
  };

  return (
    <div>
      <Link to="/">
        <Fab style={{ marginRight: 10 }} color="primary">
          <ArrowBack />
        </Fab>
      </Link>
      <span>QR Scanner</span>

      <center>
        <div style={{ marginTop: 30 }}>
          <QrScan
            delay={300}
            onError={handleError}
            onScan={handleScan}
            style={{ height: 240, width: 320 }}
          />
        </div>
      </center>

      <TextareaAutosize
        style={{ fontSize: 18, width: 320, height: 100, marginTop: 100 }}
        rowsMax={4}
        defaultValue={qrscan}
        value={qrscan}
      />
    </div>
  );
};

export default QRscanner;

You can define a scan array using useState您可以使用 useState 定义扫描数组

const [scanResults, setScanResults] = useState([])l
const handleScan = (data) => {
  if (data) {
    setScanResults([...scanResults, data]);
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 React Native 如何从图库中读取二维码 - React Native how can I read QR code from gallery HTML5-视频捕获-QR码扫描-如何开始自动对焦? - HTML5 - video capture - QR code scanning - how can I start auto focus? 我们可以在二维码中保存/存储多少数据/信息? - How much data / information can we save / store in a QR code? 使用网络摄像头扫描QR码,从wencam提取图像 - scanning qr code using webcam, fetching the image from the wencam 如何在 JS 中使用 ZXing 从网络摄像头扫描二维码? - How can I scan a QR code from a webcam using ZXing in JS? 如何在HTML中实现QR代码? - how can I implement a qr code in HTML? 如何将数据从 API url 转换为 React 中的数组? - How can I convert the data from an API url into an array in React? 如何获取存储在数据库中的数据并将其显示为二维码?, - How can I retrieve data stored in my database and display it as a qr code?, 如何使用钩子在本机反应中保存/下载生成二维码? - How to save/download generate QR Code in react native using hooks? 如何使用 react js context api 正确存储和检索数据? 我的代码没有按预期工作 - How to properly store and retrieve data using react js context api? The code, I have is not working as expected
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM