Video-React Player
Video-React is an open-source web video player library constructed using the React JavaScript library, specifically designed for modern HTML5 video environments. It provides a comprehensive suite of React components for building customizable video player interfaces, handling core functionalities such as playback control, volume management, progress indication, and full-screen toggling. The current stable version, 0.16.0, supports a broad range of React versions up to 18.0.0. While not following a strict time-based release cadence, the project actively maintains the library, with recent updates ensuring compatibility with newer React versions and addressing bugs. Its primary differentiators are its React-centric architecture, allowing for seamless integration into React applications, and its visually appealing default styling, which is heavily inspired by the well-known video.js project. Users must manually install `react` and `react-dom` as peer dependencies.
Common errors
-
Module not found: Can't resolve 'video-react/dist/video-react.css'
cause The CSS file for `video-react` was not found at the specified path, often due to a typo or incorrect import statement.fixVerify the CSS import path: `import 'video-react/dist/video-react.css';`. Ensure `video-react` is correctly installed in `node_modules`. -
Error: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components)... Check the render method of `YourComponent`.
cause This error typically indicates that a `video-react` component, such as `Player` or `Source`, was not correctly imported or used where a React element was expected.fixEnsure `Player` and other `video-react` components are correctly imported using named imports (e.g., `import { Player } from 'video-react';`) and are used as JSX components `<Player>...</Player>`. -
TypeError: Cannot read properties of undefined (reading 'play')
cause This error often occurs when attempting to call methods on the `Player` instance (e.g., using a ref) before the component has fully mounted or if the ref is not correctly assigned.fixEnsure you are using `useRef` (for function components) or creating a class ref, and that you are accessing player methods only after the component has mounted, typically within `useEffect` or `componentDidMount`. -
Webpack compilation error: 'require' is not defined in ES module scope
cause Attempting to use CommonJS `require()` syntax in an ES Module (ESM) context, which is the standard for modern React applications.fixReplace all `require()` statements for `video-react` components with ES Module `import` statements (e.g., `import { Player } from 'video-react';`).
Warnings
- breaking The component named `PlaybackRate` was renamed to `PlaybackRateMenuButton` in version `0.7.0` to better reflect its functionality as a menu button. Code using the old name will break.
- gotcha Video-React requires its CSS styles to be explicitly imported into your application for the player to render correctly. Without the CSS, the player will be unstyled and potentially non-functional.
- gotcha The `video-react` package lists `react` and `react-dom` as peer dependencies. These must be installed separately in your project, ensuring their versions are compatible with the ranges specified by `video-react` to avoid conflicts or unexpected behavior.
- gotcha Video-React officially supports only the latest stable versions of major browsers (Chrome, Firefox, Edge, Safari on Mac/iOS, Android native browsers). Older browser versions or 'untested' environments (like IE11 or specific Linux/Android setups) may exhibit unexpected behavior or require custom polyfills.
Install
-
npm install video-react -
yarn add video-react -
pnpm add video-react
Imports
- Player
const { Player } = require('video-react');import { Player } from 'video-react'; - CSS Styles
require('video-react/dist/video-react.css'); // While technically works in some bundlers, ESM import is idiomatic.import 'video-react/dist/video-react.css';
- Source
import Source from 'video-react/lib/components/Source';
import { Player, Source } from 'video-react'; - ControlBar
import { Player, ControlBar, PlayToggle } from 'video-react'; - PlaybackRateMenuButton
import { PlaybackRate } from 'video-react';import { PlaybackRateMenuButton } from 'video-react';
Quickstart
import React from 'react';
import { Player, ControlBar, PlayToggle, VolumeMenuButton, BigPlayButton, Source } from 'video-react';
import 'video-react/dist/video-react.css'; // Don't forget to import the CSS
const VideoPlayer = () => {
return (
<div style={{ width: '80%', margin: '20px auto' }}>
<Player
playsInline
poster="https://video-react.github.io/assets/poster.png"
src="https://media.w3.org/2010/05/sintel/trailer_hd.mp4"
>
<BigPlayButton position="center" />
<ControlBar autoHide={false} disableDefaultControls={false}>
<PlayToggle />
<VolumeMenuButton />
{/* Add other control bar components as needed */}
</ControlBar>
{/* You can also specify sources as children, for multiple formats */}
{/* <Source src="https://media.w3.org/2010/05/sintel/trailer_hd.mp4" type="video/mp4" /> */}
{/* <Source src="https://media.w3.org/2010/05/sintel/trailer_hd.webm" type="video/webm" /> */}
</Player>
</div>
);
};
export default VideoPlayer;