# Video.js v10 — React API (complete) > Every React API page in one file (about 52k tokens). Index with descriptions: https://videojs.org/docs/framework/react/reference/api/llms.txt --- # createPlayer Factory function that creates a typed Player component and hooks ## Import ```tsx import { createPlayer } from "@videojs/react"; import { videoFeatures } from "@videojs/react/video"; ``` `createPlayer` is the entry point for setting up a Video.js player in React. It accepts a configuration object with a `features` array and returns hooks and components for building a player. The hook is typed according to the provided features, giving you full type safety for state selectors and actions. ```tsx import { Container, createPlayer } from '@videojs/react'; import { videoFeatures } from '@videojs/react/video'; const { Player, usePlayer, useMedia } = createPlayer({ features: videoFeatures, }); // Container is imported independently from createPlayer. ``` ## Examples ### Basic Usage **App.tsx** ```tsx import { Container, createPlayer } from '@videojs/react'; import { Video, videoFeatures } from '@videojs/react/video'; const { Player, usePlayer } = createPlayer({ features: videoFeatures, }); function Controls() { const store = usePlayer(); const paused = usePlayer((s) => s.paused); return (
); } export default function BasicUsage() { return ( ); } ``` **App.css** ```css .media-container { position: relative; } .media-container video { width: 100%; } .controls { position: absolute; bottom: 10px; left: 10px; } .button { padding-block: 8px; padding-inline: 20px; color: black; cursor: pointer; background: rgba(255, 255, 255, 0.7); border: 1px solid rgba(255, 255, 255, 0.3); border-radius: 9999px; backdrop-filter: blur(10px); } ``` ## API Reference ### Video `createPlayer(config): CreatePlayerResult` Create a player instance with a typed Player component and hooks. #### Parameters | Parameter | Type | Default | Description | | --- | --- | --- | --- | | `config` (required) | `{ features: [PlayerFeature, PlayerFeature, PlayerFeature, PlayerFeature, PlayerFeature, PlayerFeature, PlayerFeature, PlayerFeature, PlayerFeature, PlayerFeature, PlayerFeature, PlayerFeature, PlayerFeature, PlayerFeature, typeof metadataFeature]; displayName?: string }` | — | Player configuration with features and optional display name. | #### Return Value | Property | Type | Description | | --- | --- | --- | | `Player` | `FC>>` | Provides a new player store to its descendants without adding a layout element. | | `usePlayer` | `{ (): VideoPlayerStore; (selector: ((state: VideoPlayerStore['state']) => R)): R }` | Accesses the configured store, or subscribes to a selected value from it. | | `useMedia` | `(() => Media \| null)` | Returns the media currently attached beneath the generated Player, or `null` before attachment. | ### Audio `createPlayer(config): CreatePlayerResult` Create a player for audio media. #### Parameters | Parameter | Type | Default | Description | | --- | --- | --- | --- | | `config` (required) | `{ features: [PlayerFeature, PlayerFeature, PlayerFeature, PlayerFeature, PlayerFeature, PlayerFeature, PlayerFeature, typeof metadataFeature]; displayName?: string }` | — | Player configuration with features and optional display name. | #### Return Value | Property | Type | Description | | --- | --- | --- | | `Player` | `FC>>` | Provides a new player store to its descendants without adding a layout element. | | `usePlayer` | `{ (): AudioPlayerStore; (selector: ((state: AudioPlayerStore['state']) => R)): R }` | Accesses the configured store, or subscribes to a selected value from it. | | `useMedia` | `(() => Media \| null)` | Returns the media currently attached beneath the generated Player, or `null` before attachment. | ### Generic `createPlayer(config): CreatePlayerResult>` Create a player with custom features. #### Parameters | Parameter | Type | Default | Description | | --- | --- | --- | --- | | `config` (required) | `{ features: Features; displayName?: string }` | — | Player configuration with features and optional display name. | #### Return Value | Property | Type | Description | | --- | --- | --- | | `Player` | `FC>>>` | Provides a new player store to its descendants without adding a layout element. | | `usePlayer` | `{ (): PlayerStore; (selector: ((state: PlayerStore['state']) => R)): R }` | Accesses the configured store, or subscribes to a selected value from it. | | `useMedia` | `(() => Media \| null)` | Returns the media currently attached beneath the generated Player, or `null` before attachment. | --- # usePlayer Hook to access the player store from within a Player ## Import ```tsx import { usePlayer } from "@videojs/react"; ``` The `usePlayer` hook returned by [`createPlayer`](https://videojs.org/docs/framework/react/reference/api/create-player) gives you access to the player store from any component within its `Player`. It’s typed to the features you passed to `createPlayer`, and has two overloads — without arguments for direct store access, or with a selector for reactive state subscriptions. ```tsx const { Player, usePlayer } = createPlayer({ features: videoFeatures }); ``` The pair works exactly like React Context: [`Player`](https://videojs.org/docs/framework/react/reference/components/player) is the Provider, and `usePlayer` is its `useContext`-style consumer, so the calling component must render inside `Player`. You can still read state anywhere: `Player` renders no DOM of its own, so lift it above every component that needs player state, the way you’d lift any Provider. `usePlayer` is also available as a standalone import (`import { usePlayer } from '@videojs/react'`), but the standalone version returns an untyped `UnknownStore`. Pass a premade selector to recover typing: ```tsx import { usePlayer, selectPlayback } from '@videojs/react'; usePlayer(); // UnknownStore (state: unknown) usePlayer(selectPlayback); // MediaPlaybackState | undefined ``` ## Examples ### Store Access Call `usePlayer()` without arguments to get the store instance. Use this for imperative actions like play, pause, and volume changes. The component does not re-render on state changes. **App.tsx** ```tsx import { Container, createPlayer } from '@videojs/react'; import { Video, videoFeatures } from '@videojs/react/video'; const { Player, usePlayer } = createPlayer({ features: videoFeatures, }); function Controls() { const store = usePlayer(); return (
); } export default function StoreAccess() { return ( ); } ``` **App.css** ```css .media-container { position: relative; } .media-container video { width: 100%; } .controls { display: flex; gap: 6px; padding: 12px; background: rgba(0, 0, 0, 0.05); border-top: 1px solid rgba(0, 0, 0, 0.1); } .controls button { padding: 4px 12px; font-size: 0.8125rem; color: #111827; cursor: pointer; background: white; border: 1px solid #ccc; border-radius: 6px; } ``` ### Selector Subscription Pass a selector function to subscribe to specific state. The component re-renders when the selected value changes, using shallow equality by default. **App.tsx** ```tsx import { Container, createPlayer } from '@videojs/react'; import { Video, videoFeatures } from '@videojs/react/video'; const { Player, usePlayer } = createPlayer({ features: videoFeatures, }); function StateDisplay() { const state = usePlayer((s) => ({ paused: s.paused, currentTime: s.currentTime, duration: s.duration, })); return (
Paused
{String(state.paused)}
Time
{state.currentTime.toFixed(1)}s / {state.duration.toFixed(1)}s
); } export default function Selector() { return ( ); } ``` **App.css** ```css .media-container { position: relative; } .media-container video { width: 100%; } .panel { display: flex; gap: 16px; padding: 12px; margin: 0; font-size: 0.8125rem; background: rgba(0, 0, 0, 0.05); border-top: 1px solid rgba(0, 0, 0, 0.1); } .panel div { display: flex; gap: 8px; } .panel dt { color: #6b7280; } .panel dd { margin: 0; font-variant-numeric: tabular-nums; } ``` ## API Reference ### Without Selector `usePlayer(): UnknownStore` Access the player store from within a Player. This standalone hook has no knowledge of your configured features, so it returns an untyped `UnknownStore` whose state properties are typed as `unknown`. For typed access, use the `usePlayer` returned by `createPlayer()`, or pass a premade selector to recover the type from its return value. #### Return Value | Property | Type | | --- | --- | | `$state` | `{ current: Readonly; subscribe(callback: (() => void), options?: SubscribeOptions): (() => void) }` | | `target` | `unknown \| null` | | `destroyed` | `boolean` | | `state` | `Record` | | `attach` | `((target: unknown) => (() => void))` | | `destroy` | `(() => void)` | | `subscribe` | `((callback: StateChange, options?: SubscribeOptions) => (() => void))` | ### With Selector `usePlayer(selector): R` Select a value from the player store. Re-renders when the selected value changes. The selector receives `UnknownState`, so an inline selector returns `unknown`. Pass a premade selector (e.g. `selectPlayback`) to get a typed result. #### Parameters | Parameter | Type | Default | Description | | --- | --- | --- | --- | | `selector` (required) | `((state: UnknownState) => R)` | — | Derives a value from the player store state. | #### Return Value `R` --- # useOptionalPlayer Hook to access or select Player state when a Player is available ## Import ```tsx import { useOptionalPlayer } from "@videojs/react"; ``` ## Usage Without a selector, the hook returns the player store or `undefined` outside `Player`. ```tsx const store = useOptionalPlayer(); ``` With a selector, it subscribes while a player is available and returns the selected value. The selector is not called outside `Player`. ```tsx import { selectPlayback, useOptionalPlayer } from "@videojs/react"; const playback = useOptionalPlayer(selectPlayback); ``` Use `usePlayer` when the component requires a surrounding player. ## API Reference ### Without Selector `useOptionalPlayer(): UnknownStore | undefined` Returns the player store when available, or `undefined` outside a Player. #### Return Value | Property | Type | | --- | --- | | `$state` | `{ current: Readonly; subscribe(callback: (() => void), options?: SubscribeOptions): (() => void) }` | | `target` | `unknown \| null` | | `destroyed` | `boolean` | | `state` | `Record` | | `attach` | `((target: unknown) => (() => void))` | | `destroy` | `(() => void)` | | `subscribe` | `((callback: StateChange, options?: SubscribeOptions) => (() => void))` | ### With Selector `useOptionalPlayer(selector): R | undefined` Selects a player value when available, or returns `undefined` outside a Player. #### Parameters | Parameter | Type | Default | Description | | --- | --- | --- | --- | | `selector` (required) | `((state: UnknownState) => R)` | — | Derives a value from the player store state. | #### Return Value `R | undefined` --- # useMedia Hook to access the media attached to the nearest Player `useMedia` returns the media attached to the nearest `Player`, or `null` until a media component mounts inside it. ## Import ```tsx import { useMedia } from "@videojs/react"; ``` [`createPlayer`](https://videojs.org/docs/framework/react/reference/api/create-player) also returns `useMedia` alongside `Player` and `usePlayer`: ```tsx const { Player, usePlayer, useMedia } = createPlayer({ features: videoFeatures }); ``` Both forms are the same hook and return the same `Media | null` type. Unlike [`usePlayer`](https://videojs.org/docs/framework/react/reference/api/use-player), the `createPlayer` form doesn’t add typing from your features. ## Usage Call `useMedia` from a component rendered inside `Player`. Calling it outside `Player` throws, because it reads the nearest `Player`’s context. **PlayFromStart.tsx** ```tsx import { isMediaSeekCapable, useMedia } from "@videojs/react"; export function PlayFromStart() { const media = useMedia(); function playFromStart() { if (isMediaSeekCapable(media)) media.currentTime = 0; media?.play(); } return ; } ``` The component re-renders when a different media attaches or the current one detaches, not when the media’s own properties change. To render from playback state, such as `paused` or `currentTime`, select it with [`usePlayer`](https://videojs.org/docs/framework/react/reference/api/use-player). ## The `Media` object `Media` is the player’s handle on whatever plays the content. What that handle is depends on the media component: - `Video` and `Audio` attach the rendered native `