Skip to content

useScreenCapture

This hook uses the user screen capture and returns its states and controls as well as the media stream object.

Options

  • It takes a getDisplayMedia constraints, as well as a video element ref object.

Return Values

NameTypeDescription
refRefObjecta <video> element reference.
streamRefObjectthe MediaStream object reference.
isEnabledBooleanindicates if the screen capture is enabled or not.
errorUnknowncached error if it exists.
startFunctionan async function to start streaming.
stopFunctiona function to stop streaming.
toggleFunctiontoggle between start and stop, and it can take a boolean to force it.

Example Usage

tsx
import { useScreenCapture } from 'react-pre-hooks';

export default function Example() {
  const capture = useScreenCapture();

  return (
    <main>
      <button onClick={capture.toggle}>
        {capture.isEnabled ? 'stop' : 'start'}
      </button>
      <video ref={capture.ref} />
    </main>
  );
}
import { useScreenCapture } from 'react-pre-hooks';

export default function Example() {
  const capture = useScreenCapture();

  return (
    <main>
      <button onClick={capture.toggle}>
        {capture.isEnabled ? 'stop' : 'start'}
      </button>
      <video ref={capture.ref} />
    </main>
  );
}

Released under the MIT License.