'Can I open a mobile device camera with React.js?

I need my QR code scanner to scan codes using a mobile device. At the moment the app works on a laptop/desktop webcam, but its intended purpose is to be used by a mobile device. From my own research, it looks like this is easy from React native, but I have no experience with react native. Is there some way I can achieve my goal with React JS? Or do I have to convert my React app somehow to React Native? I'm a beginner, so the simplest way possible would be great. Here's my QR code scanner component

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";
import { useToolProvider } from "../contexts/tool";

const QRscanner = () => {
  const [qrscan, setQrscan] = useState("no result");
  const { state, updateInventory } = useToolProvider();

  const handleError = (err) => {
    console.error(err);
  };
  const [scanResults, setScanResults] = useState();
  const [scanTrigger, setScanTrigger] = useState(false);
  const qrScanTrigger = () => {
    setScanTrigger(true);
  };
  const handleScan = (data) => {
    if (data) {
      setScanResults(data);
    }
  };
  const submit = () => {
    updateInventory(scanResults);
  };

  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 }}
            reactivateTimeout={500}
            onEvent={qrScanTrigger}
          />
        </div>
      </center>

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

        {scanTrigger && <h1>it worked</h1>}
        <button onClick={submit}>Submit</button>
        <p>{JSON.stringify(state)}</p>
      </div>
    </div>
  );
};

export default QRscanner;


Solution 1:[1]

Instead of using a recursive function flatten the array and then use reduce to calculate the sum.

const arr = [4, [5, 7, [12, [1, 2]]]];

function calculateSum(arr) {
  return arr.flat(Infinity).reduce(function (acc, c) {
    return acc + c;
  }, 0);
}

console.log(calculateSum(arr));

Solution 2:[2]

You are passing in a boolean not an array when you do:

acc= acc + calculateSum(isEntryArray)
                        // ^^^ from Array.isArray()

I believe what you wanted was to pass in the current sub array

acc= acc + calculateSum(currentVal)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 charlietfl