Title: Illegal constructor · Issue #154 · Dynamsoft/barcode-reader-javascript · GitHub
Open Graph Title: Illegal constructor · Issue #154 · Dynamsoft/barcode-reader-javascript
X Title: Illegal constructor · Issue #154 · Dynamsoft/barcode-reader-javascript
Description: Hello, Short version: maybe I am just blind after some hours spent on that. There is an annoying "Illegal constructor" appearing and I cannot figure out why. I am trying to set up the React VideoCapture element as mentioned here. It look...
Open Graph Description: Hello, Short version: maybe I am just blind after some hours spent on that. There is an annoying "Illegal constructor" appearing and I cannot figure out why. I am trying to set up the React VideoCa...
X Description: Hello, Short version: maybe I am just blind after some hours spent on that. There is an annoying "Illegal constructor" appearing and I cannot figure out why. I am trying to set up the Rea...
Opengraph URL: https://github.com/Dynamsoft/barcode-reader-javascript/issues/154
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Illegal constructor","articleBody":"Hello,\r\n\r\nShort version: maybe I am just blind after some hours spent on that. There is an annoying \"Illegal constructor\" appearing and I cannot figure out why.\r\n\r\nI am trying to set up the React `VideoCapture` element as [mentioned here](https://github.com/Dynamsoft/barcode-reader-javascript-samples/tree/v10.4.20/hello-world/react#create-and-edit-the-videocapture-component).\r\n\r\nIt looks like this:\r\n\r\n\u003cdetails>\r\n\r\n\u003csummary>`VideoCapture` element\u003c/summary>\r\n\r\n```tsx\r\nimport React from 'react';\r\n\r\nimport '../dynamsoft.config';\r\n\r\n// --Change starts\r\n// For some reason, import from Dynamsoft does not work. But the library is creating a global\r\n// `Dynamsoft` object that has all the library contents.\r\n\r\n// @ts-ignore\r\nimport \"dynamsoft-camera-enhancer\";\r\n// @ts-ignore\r\nimport \"dynamsoft-capture-vision-router\";\r\n// @ts-ignore\r\nimport \"dynamsoft-utility\";\r\n\r\nconst Dynamsoft = (window as any).Dynamsoft;\r\nconst CameraEnhancer = Dynamsoft.DCE.CameraEnhancer;\r\nconst CameraView = Dynamsoft.DCE.CameraView;\r\nconst CaptureVisionRouter = Dynamsoft.CVR.CaptureVisionRouter;\r\nconst MultiFrameResultCrossFilter = Dynamsoft.Utility.MultiFrameResultCrossFilter;\r\n\r\n// -- Change end\r\n\r\nconst componentDestroyedErrorMsg = \"VideoCapture Component Destroyed\";\r\n\r\nclass VideoCapture extends React.Component\u003c{ onSubmit: Function }, {}> {\r\n cameraViewContainer: React.RefObject\u003cHTMLDivElement> = React.createRef();\r\n\r\n resolveInit?: () => void;\r\n pInit: Promise\u003cvoid> = new Promise((r) => (this.resolveInit = r));\r\n isDestroyed = false;\r\n\r\n cvRouter?: typeof Dynamsoft.CVR;\r\n cameraEnhancer?: typeof Dynamsoft.CE;\r\n\r\n async componentDidMount() {\r\n try {\r\n // Create a `CameraEnhancer` instance for camera control and a `CameraView` instance for UI control.\r\n const cameraView = await CameraView.createInstance();\r\n if (this.isDestroyed) {\r\n throw Error(componentDestroyedErrorMsg);\r\n } // Check if component is destroyed after every async\r\n\r\n this.cameraEnhancer = await CameraEnhancer.createInstance(cameraView);\r\n if (this.isDestroyed) {\r\n throw Error(componentDestroyedErrorMsg);\r\n }\r\n\r\n // Get default UI and append it to DOM.\r\n this.cameraViewContainer.current!.append(cameraView.getUIElement());\r\n\r\n // Create a `CaptureVisionRouter` instance and set `CameraEnhancer` instance as its image source.\r\n this.cvRouter = await CaptureVisionRouter.createInstance();\r\n if (this.isDestroyed) {\r\n throw Error(componentDestroyedErrorMsg);\r\n }\r\n this.cvRouter.setInput(this.cameraEnhancer);\r\n\r\n // Define a callback for results.\r\n this.cvRouter.addResultReceiver({\r\n onDecodedBarcodesReceived: (result: any) => {\r\n if (!result.barcodeResultItems.length) return;\r\n\r\n for (let item of result.barcodeResultItems) {\r\n this.props.onSubmit(item.text);\r\n }\r\n },\r\n });\r\n\r\n // Filter out unchecked and duplicate results.\r\n const filter = new MultiFrameResultCrossFilter();\r\n // Filter out unchecked barcodes.\r\n filter.enableResultCrossVerification(\"barcode\", true);\r\n // Filter out duplicate barcodes within 3 seconds.\r\n filter.enableResultDeduplication(\"barcode\", true);\r\n await this.cvRouter.addResultFilter(filter);\r\n if (this.isDestroyed) {\r\n throw Error(componentDestroyedErrorMsg);\r\n }\r\n\r\n // Open camera and start scanning single barcode.\r\n await this.cameraEnhancer.open();\r\n if (this.isDestroyed) {\r\n throw Error(componentDestroyedErrorMsg);\r\n }\r\n await this.cvRouter.startCapturing(\"ReadSingleBarcode\");\r\n if (this.isDestroyed) {\r\n throw Error(componentDestroyedErrorMsg);\r\n }\r\n } catch (ex: any) {\r\n if ((ex as Error)?.message === componentDestroyedErrorMsg) {\r\n console.log(componentDestroyedErrorMsg);\r\n } else {\r\n let errMsg = ex.message || ex;\r\n console.error(errMsg);\r\n console.log(\"ERROR HERE\");\r\n console.log(ex);\r\n alert(errMsg);\r\n }\r\n }\r\n\r\n // Resolve pInit promise once initialization is complete.\r\n this.resolveInit!();\r\n }\r\n\r\n async componentWillUnmount() {\r\n this.isDestroyed = true;\r\n try {\r\n // Wait for the pInit to complete before disposing resources.\r\n await this.pInit;\r\n this.cvRouter?.dispose();\r\n this.cameraEnhancer?.dispose();\r\n } catch (_) {}\r\n }\r\n\r\n shouldComponentUpdate() {\r\n // Never update UI after mount, sdk use native way to bind event, update will remove it.\r\n return false;\r\n }\r\n\r\n render() {\r\n return (\r\n \u003cdiv ref={this.cameraViewContainer} style={{ width: \"100%\", height: \"70vh\" }}>\u003c/div>\r\n );\r\n }\r\n}\r\n\r\nexport default VideoCapture;\r\n```\r\n\r\n\u003c/details>\r\n\r\nThe `VideoCapture` component is embedded is another `QRCodeReader` element.\r\n\r\n\u003cdetails>\r\n\r\n\u003csummary>`QRCodeReader` element\u003c/summary>\r\n\r\n```jsx\r\nimport { useState } from 'react';\r\nimport VideoCapture from \"./VideoCapture\";\r\n\r\nconst QRCodeReader = (props) => {\r\n const [errors, setErrors] = useState(null);\r\n\r\n const onSubmit = async (param) => {\r\n try {\r\n const isUpdated = await props.onSubmit(param);\r\n if (isUpdated) {\r\n setErrors(null);\r\n }\r\n } catch(error){\r\n setErrors(error);\r\n }\r\n }\r\n\r\n return (\r\n \u003cdiv>\r\n \u003cVideoCapture onSubmit={onSubmit} />\r\n {errors}\r\n \u003c/div>\r\n );\r\n}\r\n\r\nexport default QRCodeReader;\r\n```\r\n\r\n\u003c/details>\r\n\r\nUntil here, everything works well. Using the `QRCodeReader` element, it can scan QRCode and extract the item elements. Nice.\r\n\r\nThe issue happens after that, when I try to embed the `QRCodeReader` element into a `QRModal` Element.\r\nElements looks like this `VideoCapture \u003c- QRCodeReader \u003c- QRModal`.\r\n\r\nWe want to use them in a process that is the following: \r\n- first scan: uses the `QRCodeReader` (full page reader).\r\n- updates the page to match info in the QRCode, loads a list of items from db on the page, each item has a scan modal button to trigger the `QRModal`, to access the detail of each item, we must scan again using the modal.\r\n- second scan: uses the `QRModal` (modal reader) -> `fails consistently with the same error`.\r\n\r\n\u003cdetails>\r\n\r\n\u003csummary>`QRModal` element\u003c/summary>\r\n\r\n```jsx\r\nimport React, {useEffect, useState} from 'react';\r\nimport PropTypes from 'prop-types';\r\nimport Modal from 'Modal';\r\nimport QRCodeReader from './QRCodeReader';\r\n\r\n/**\r\n * Widget to access a profile using a QR code\r\n *\r\n * @param {object} props - React props\r\n *\r\n * @return {ReactDOM}\r\n */\r\nconst QRModal = (props) => {\r\n const [code, setCode] = useState(null);\r\n\r\n useEffect(() => {\r\n }, []);\r\n\r\n return (\r\n \u003cModal\r\n onClose={() => {\r\n props.onClose();\r\n }}\r\n show={true}\r\n title={props.modalTitle}\r\n >\r\n \u003cQRCodeReader onSubmit={(c) => {\r\n if (c === code) return;\r\n setCode(c);\r\n props.onScan(c);\r\n }} />\r\n \u003c/Modal>\r\n );\r\n}\r\n\r\nQRModal.defaultProps = {\r\n modalTitle: 'Scan QR Code',\r\n};\r\n\r\nQRModal.propTypes = {\r\n modalTitle: PropTypes.string,\r\n};\r\n\r\nexport default QRModal;\r\n```\r\n\r\n\u003c/details>\r\n\r\n\r\nThis is throwing a `Type Error: Illegal constructor.` apparently coming from `CameraView.createInstance()`.\r\n\r\nI also tried to directly shortcut QRModal to directly call another VideoCapture class (`VideoCapture2` is a copy of `VideoCapture`). Same result.\r\n\r\n\r\n\r\n","author":{"url":"https://github.com/regisoc","@type":"Person","name":"regisoc"},"datePublished":"2024-11-15T15:21:15Z","url":"https://github.com/Dynamsoft/barcode-reader-javascript/issues/154"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:3517785c-17d2-f4f6-0d1b-03bff4f61dfe |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | A42A:1F7384:1322FFB:194C6AE:6B011E5A |
| html-safe-nonce | 8c6b0e3bb927e6d917c5e2c52c39a867e700dd90a17a91bf619dadd57e468210 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJBNDJBOjFGNzM4NDoxMzIyRkZCOjE5NEM2QUU6NkIwMTFFNUEiLCJ2aXNpdG9yX2lkIjoiMTkxOTE3MjQ4MTYyNjM1MzU0IiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | 6082524904277f8901f8a9f8d87b7af51ea240ea8a338f720b843df42f745733 |
| hovercard-subject-tag | issue:2662288283 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/Dynamsoft/barcode-reader-javascript/154/issue_layout |
| twitter:image | https://opengraph.githubassets.com/c035d528b791dcb6b7d9999f686d7f4522d0d686b4dbd2d75e97cd9e597fce57/Dynamsoft/barcode-reader-javascript/issues/154 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/c035d528b791dcb6b7d9999f686d7f4522d0d686b4dbd2d75e97cd9e597fce57/Dynamsoft/barcode-reader-javascript/issues/154 |
| og:image:alt | Hello, Short version: maybe I am just blind after some hours spent on that. There is an annoying "Illegal constructor" appearing and I cannot figure out why. I am trying to set up the React VideoCa... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | regisoc |
| hostname | github.com |
| expected-hostname | github.com |
| None | cc5dfdd32f62ebd3223cab3af3aa2b268d3605218237326a48a99d1efd89495c |
| turbo-cache-control | no-preview |
| go-import | github.com/Dynamsoft/barcode-reader-javascript git https://github.com/Dynamsoft/barcode-reader-javascript.git |
| octolytics-dimension-user_id | 5820187 |
| octolytics-dimension-user_login | Dynamsoft |
| octolytics-dimension-repository_id | 145683056 |
| octolytics-dimension-repository_nwo | Dynamsoft/barcode-reader-javascript |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 145683056 |
| octolytics-dimension-repository_network_root_nwo | Dynamsoft/barcode-reader-javascript |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 8c77c9e8439031ad54c75a250694842892f693e1 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width