[英]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.