Core Concepts
State Management
In this section, we'll look at the React hooks available for reading and updating media state.
Setup
The <MediaProvider>
is required to enable using media hooks:
Reading
The media state hook enables you to subscribe directly to specific media state changes, rather than listening to potentially multiple DOM events and binding it yourself.
Tracking media state via events:
Tracking media state via hook:
Your IDE should provide helpful suggestions and docs on the media props that are available. You can also use the MediaState
interface on GitHub as a reference.
Avoiding Renders
Media state can be directly accessed on the <Media>
component if you'd like to avoid unnecessary re-renders:
Or, if inside a child of <MediaProvider>
:
Updating
The media remote hook provides a simple facade for dispatching media request events. This can be used to request media playback to play/pause, change the current volume level, seek to a different time position, and other actions that change media state.
Event Triggers
Event triggers enable connecting media events back to their origin event. This can be useful when trying to understand how a media event was triggered, or when analyzing data such as the time difference between the request and when the media was actually played.
Media Element
The following hook provides you with a reference to the nearest parent MediaElement
(i.e., <vds-media>
).
Avoid calling methods directly on the media element:
Media Provider
The following hook provides you with a reference to the nearest parent MediaProviderElement
(e.g., <vds-video>
or <vds-hls-video>
).