11import { useState , useCallback } from 'react' ;
2- import { useCameraDevice , useCodeScanner } from 'react-native-vision-camera' ;
2+ import {
3+ isScannedCode ,
4+ useCameraDevice ,
5+ useObjectOutput ,
6+ } from 'react-native-vision-camera' ;
7+ import type { ScannedObject } from 'react-native-vision-camera' ;
38import { useCameraPermission } from './useCameraPermission' ;
49
510export type QRScanResult =
@@ -15,20 +20,19 @@ export interface UseQRScannerOptions {
1520export interface UseQRScannerReturn {
1621 device : ReturnType < typeof useCameraDevice > ;
1722 hasPermission : boolean | null ;
18- isLoading : boolean ;
1923 requestPermission : ( ) => Promise < boolean > ;
2024 isActive : boolean ;
2125 setIsActive : ( active : boolean ) => void ;
2226 scannedData : QRScanResult | null ;
23- codeScanner : ReturnType < typeof useCodeScanner > ;
27+ objectOutput : ReturnType < typeof useObjectOutput > ;
2428}
2529
2630export function useQRScanner (
2731 options : UseQRScannerOptions = { } ,
2832) : UseQRScannerReturn {
2933 const { parseJSON = false , onScan, onError } = options ;
3034
31- const { hasPermission, isLoading , requestPermission } = useCameraPermission ( ) ;
35+ const { hasPermission, requestPermission } = useCameraPermission ( ) ;
3236 const device = useCameraDevice ( 'back' ) ;
3337 const [ isActive , setIsActive ] = useState ( false ) ;
3438 const [ scannedData , setScannedData ] = useState < QRScanResult | null > ( null ) ;
@@ -49,12 +53,10 @@ export function useQRScanner(
4953 [ parseJSON ] ,
5054 ) ;
5155
52- const handleCodeScanned = useCallback (
53- ( codes : any [ ] ) => {
54- if ( codes . length === 0 ) return ;
55-
56- const code = codes [ 0 ] ;
57- const rawValue = code . value ;
56+ const handleObjectsScanned = useCallback (
57+ ( objects : ScannedObject [ ] ) => {
58+ const code = objects . find ( isScannedCode ) ;
59+ const rawValue = code ?. value ;
5860
5961 if ( ! rawValue ) return ;
6062
@@ -80,19 +82,18 @@ export function useQRScanner(
8082 [ parseQRData , onScan , onError ] ,
8183 ) ;
8284
83- const codeScanner = useCodeScanner ( {
84- codeTypes : [ 'qr' ] ,
85- onCodeScanned : handleCodeScanned ,
85+ const objectOutput = useObjectOutput ( {
86+ types : [ 'qr' ] ,
87+ onObjectsScanned : handleObjectsScanned ,
8688 } ) ;
8789
8890 return {
8991 device,
9092 hasPermission,
91- isLoading,
9293 requestPermission,
9394 isActive,
9495 setIsActive,
9596 scannedData,
96- codeScanner ,
97+ objectOutput ,
9798 } ;
9899}
0 commit comments