Skip to content

Commit 6285eba

Browse files
authored
Merge pull request #231 from Dynamsoft/fix/react-callback
fix: remove useCallback on ImageDecode components for React and Next
2 parents fa9e06d + fa03aed commit 6285eba

File tree

4 files changed

+22
-22
lines changed

4 files changed

+22
-22
lines changed

hello-world/next/README.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ export default VideoCapture;
247247

248248
```tsx
249249
/* /components/ImageCapture/ImageCapture.tsx */
250-
import React, { useRef, useEffect, MutableRefObject, useCallback } from "react";
250+
import React, { useRef, useEffect, MutableRefObject } from "react";
251251
import "../../dynamsoft.config"; // import side effects. The license, engineResourcePath, so on.
252252
import { EnumCapturedResultItemType } from "dynamsoft-core";
253253
import { BarcodeResultItem } from "dynamsoft-barcode-reader";
@@ -259,7 +259,7 @@ function ImageCapture() {
259259
let pCvRouter: MutableRefObject<Promise<CaptureVisionRouter> | null> = useRef(null);
260260
let isDestroyed = useRef(false);
261261

262-
const decodeImg = useCallback(async (e: React.ChangeEvent<HTMLInputElement>) => {
262+
const decodeImg = async (e: React.ChangeEvent<HTMLInputElement>) => {
263263
let files = [...(e.target.files as any as File[])];
264264
e.target.value = ""; // reset input
265265
resultsContainer.current!.innerText = "";
@@ -287,26 +287,26 @@ function ImageCapture() {
287287
console.log(item.text);
288288
}
289289
// If no items are found, display that no barcode was detected
290-
if (!result.items.length) resultsContainer.current!.innerText = "No barcode found";
290+
if (!result.items.length) resultsContainer.current!.innerText += "No barcode found";
291291
}
292292
} catch (ex: any) {
293293
let errMsg = ex.message || ex;
294294
console.error(errMsg);
295295
alert(errMsg);
296296
}
297-
}, []);
297+
};
298298

299299
useEffect((): any => {
300300
// In 'development', React runs setup and cleanup one extra time before the actual setup in Strict Mode.
301301
isDestroyed.current = false;
302302

303303
// componentWillUnmount. dispose cvRouter when it's no longer needed
304-
return async () => {
304+
return () => {
305305
isDestroyed.current = true;
306306
if (pCvRouter.current) {
307-
try {
308-
(await pCvRouter.current).dispose();
309-
} catch (_) {}
307+
pCvRouter.current.then((cvRouter) => {
308+
cvRouter.dispose();
309+
}).catch((_) => { })
310310
}
311311
};
312312
}, []);

hello-world/next/components/ImageCapture/ImageCapture.tsx

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { useRef, useEffect, MutableRefObject, useCallback, useState } from "react";
1+
import React, { useRef, useEffect, MutableRefObject, useState } from "react";
22
import "../../dynamsoft.config"; // import side effects. The license, engineResourcePath, so on.
33
import { EnumCapturedResultItemType } from "dynamsoft-core";
44
import { BarcodeResultItem } from "dynamsoft-barcode-reader";
@@ -11,7 +11,7 @@ function ImageCapture() {
1111
let pCvRouter: MutableRefObject<Promise<CaptureVisionRouter> | null> = useRef(null);
1212
let isDestroyed = useRef(false);
1313

14-
const decodeImg = useCallback(async (e: React.ChangeEvent<HTMLInputElement>) => {
14+
const decodeImg = async (e: React.ChangeEvent<HTMLInputElement>) => {
1515
let files = [...(e.target.files as any as File[])];
1616
e.target.value = ""; // reset input
1717
let _resultText = "";
@@ -48,19 +48,19 @@ function ImageCapture() {
4848
console.error(errMsg);
4949
alert(errMsg);
5050
}
51-
}, []);
51+
};
5252

5353
useEffect((): any => {
5454
// In 'development', React runs setup and cleanup one extra time before the actual setup in Strict Mode.
5555
isDestroyed.current = false;
5656

5757
// componentWillUnmount. dispose cvRouter when it's no longer needed
58-
return async () => {
58+
return () => {
5959
isDestroyed.current = true;
6060
if (pCvRouter.current) {
61-
try {
62-
(await pCvRouter.current).dispose();
63-
} catch (_) {}
61+
pCvRouter.current.then((cvRouter) => {
62+
cvRouter.dispose();
63+
}).catch((_) => { })
6464
}
6565
};
6666
}, []);

hello-world/react-hooks/README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ export default VideoCapture;
224224

225225
```tsx
226226
/* /src/components/ImageCapture/ImageCapture.tsx */
227-
import React, { useRef, useEffect, MutableRefObject, useCallback } from "react";
227+
import React, { useRef, useEffect, MutableRefObject } from "react";
228228
import "../../dynamsoft.config"; // import side effects. The license, engineResourcePath, so on.
229229
import { EnumCapturedResultItemType } from "dynamsoft-core";
230230
import { BarcodeResultItem } from "dynamsoft-barcode-reader";
@@ -236,7 +236,7 @@ function ImageCapture() {
236236
let pCvRouter: MutableRefObject<Promise<CaptureVisionRouter> | null> = useRef(null);
237237
let isDestroyed = useRef(false);
238238

239-
const decodeImg = useCallback(async (e: React.ChangeEvent<HTMLInputElement>) => {
239+
const decodeImg = async (e: React.ChangeEvent<HTMLInputElement>) => {
240240
let files = [...(e.target.files as any as File[])];
241241
e.target.value = ""; // reset input
242242
resultsContainer.current!.innerText = "";
@@ -271,7 +271,7 @@ function ImageCapture() {
271271
console.error(errMsg);
272272
alert(errMsg);
273273
}
274-
}, []);
274+
};
275275

276276
useEffect((): any => {
277277
// In 'development', React runs setup and cleanup one extra time before the actual setup in Strict Mode.

hello-world/react-hooks/src/components/ImageCapture/ImageCapture.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { useRef, useEffect, MutableRefObject, useCallback, useState } from "react";
1+
import React, { useRef, useEffect, MutableRefObject, useState } from "react";
22
import "../../dynamsoft.config"; // import side effects. The license, engineResourcePath, so on.
33
import { EnumCapturedResultItemType } from "dynamsoft-core";
44
import { BarcodeResultItem } from "dynamsoft-barcode-reader";
@@ -11,7 +11,7 @@ function ImageCapture() {
1111
let pCvRouter: MutableRefObject<Promise<CaptureVisionRouter> | null> = useRef(null);
1212
let isDestroyed = useRef(false);
1313

14-
const captureImage = useCallback(async (e: React.ChangeEvent<HTMLInputElement>) => {
14+
const decodeImg = async (e: React.ChangeEvent<HTMLInputElement>) => {
1515
let files = [...(e.target.files as any as File[])];
1616
e.target.value = ""; // reset input
1717
setResultText("");
@@ -48,7 +48,7 @@ function ImageCapture() {
4848
console.error(errMsg);
4949
alert(errMsg);
5050
}
51-
}, []);
51+
};
5252

5353
useEffect((): any => {
5454
// In 'development', React runs setup and cleanup one extra time before the actual setup in Strict Mode.
@@ -68,7 +68,7 @@ function ImageCapture() {
6868
return (
6969
<div className="image-capture-container">
7070
<div className="input-container">
71-
<input type="file" multiple accept=".jpg,.jpeg,.icon,.gif,.svg,.webp,.png,.bmp" onChange={captureImage} />
71+
<input type="file" multiple accept=".jpg,.jpeg,.icon,.gif,.svg,.webp,.png,.bmp" onChange={decodeImg} />
7272
</div>
7373
<div className="results">{resultText}</div>
7474
</div>

0 commit comments

Comments
 (0)