Skip to main content

useMediaDevices

React sensor hook that tracks connected hardware devices

Usage

Live Editor
function Demo() {
  const [state] = useMediaDevices({
    requestPermissions: true,
  });

  return <pre>{JSON.stringify(state, null, 2)}</pre>;
};

Result
Loading...

API

useMediaDevices

Returns

readonly [{ devices: { deviceId: string; groupId: string; kind: MediaDeviceKind; label: string; }[]; }, () => Promise<boolean>]: A tuple with the following elements:

  • The media devices info.
  • A function to request media devices permission.

Arguments

ArgumentDescriptionTypeDefaultValue
optionsoptional paramsUseMediaDeviceOptions | undefined-

UseMediaDeviceOptions

PropertyDescriptionTypeDefaultValue
requestPermissionsRequest for permissions immediately if it's not granted,otherwise label and deviceIds could be emptybooleanfalse
constraintsRequest for types of media permissionsMediaStreamConstraints{ audio: true, video: true }