react-native-vision-camera/package/src/hooks/useCameraDevices.ts

24 lines
768 B
TypeScript
Raw Normal View History

import { useEffect, useState } from 'react'
import type { CameraDevice } from '../CameraDevice'
import { CameraDevices } from '../CameraDevices'
2021-02-20 15:20:28 -07:00
/**
* Get all available Camera Devices this phone has.
2021-02-20 15:20:28 -07:00
*
* Camera Devices attached to this phone (`back` or `front`) are always available,
* while `external` devices might be plugged in or out at any point,
* so the result of this function might update over time.
2021-02-20 15:20:28 -07:00
*/
export function useCameraDevices(): CameraDevice[] {
const [devices, setDevices] = useState(() => CameraDevices.getAvailableCameraDevices())
2021-02-20 09:05:02 -07:00
useEffect(() => {
const listener = CameraDevices.addCameraDevicesChangedListener((newDevices) => {
setDevices(newDevices)
})
return () => listener.remove()
}, [])
2021-02-20 09:05:02 -07:00
return devices
2021-02-20 15:20:28 -07:00
}