Size
0.59 kb
useDisplayMedia
React hook to access and manage screen sharing capabilities using the Screen Capture API.
Installation
Usage
Pass the media constraints to the hook.
The hook follows a simple state machine:
- Initially starts in 
"closed"state withstream: null - Calling 
open()transitions to"loading"state while requesting screen sharing access - On successful access, transitions to 
"open"state withstream: MediaStream - If an error occurs during loading, transitions to 
"error"state with the error details - Calling 
close()returns to"closed"state, stops the stream, and setsstream: null 
This predictable state flow makes it easy to build UIs that respond appropriately to the screen sharing status.
Example
Capture and display the device's screen:
API Reference
UseDisplayMediaOptions
The options of the useDisplayMedia hook. See MDN docs for more details.
| Prop | Type | Default | 
|---|---|---|
video? | boolean | MediaTrackConstraints | - | 
audio? | boolean | MediaTrackConstraints | - | 
UseDisplayMediaReturn
Return value of the useDisplayMedia hook.
| Prop | Type | Default | 
|---|---|---|
toggleVideo | () => void | - | 
toggleAudio | () => void | - | 
close | () => void | - | 
open | () => void | - | 
videoState | "none" | "enabled" | "disabled" | - | 
audioState | "none" | "enabled" | "disabled" | - | 
error | Error | null | - | 
state | "closed" | "error" | "open" | "loading" | - | 
stream | MediaStream | null | - |