Video.js and ReactJS integration

Here’s a basic ReactJS player implementation.

It just instantiates the Video.js player on componentDidMount and destroys it on componentWillUnmount.

  1. import React from 'react';
  2. import videojs from 'video.js'
  3. export default class VideoPlayer extends React.Component {
  4. componentDidMount() {
  5. // instantiate Video.js
  6. this.player = videojs(this.videoNode, this.props, function onPlayerReady() {
  7. console.log('onPlayerReady', this)
  8. });
  9. }
  10. // destroy player on unmount
  11. componentWillUnmount() {
  12. if (this.player) {
  13. this.player.dispose()
  14. }
  15. }
  16. // wrap the player in a div with a `data-vjs-player` attribute
  17. // so videojs won't create additional wrapper in the DOM
  18. // see https://github.com/videojs/video.js/pull/3856
  19. render() {
  20. return (
  21. <div>
  22. <div data-vjs-player>
  23. <video ref={ node => this.videoNode = node } className="video-js"></video>
  24. </div>
  25. </div>
  26. )
  27. }
  28. }

You can then use it like this: (see options guide for option information)

  1. const videoJsOptions = {
  2. autoplay: true,
  3. controls: true,
  4. sources: [{
  5. src: '/path/to/video.mp4',
  6. type: 'video/mp4'
  7. }]
  8. }
  9. return <VideoPlayer { ...videoJsOptions } />

Don’t forget to include the Video.js CSS, located at video.js/dist/video-js.css.

Using a React Component as a Video JS Component

  1. /**
  2. * EpisodeList.js
  3. *
  4. * This is just a plain ol' React component.
  5. * the vjsComponent methods, player methods etc. are available via
  6. * the vjsComponent prop (`this.props.vjsComponent`)
  7. */
  8. import React, { Component, PropTypes } from 'react';
  9. class EpisodeList extends Component {
  10. render() {
  11. return (
  12. <div>
  13. <h1>{this.props.body}</h1>
  14. </div>
  15. );
  16. }
  17. }
  18. /**
  19. * vjsEpisodeList.js
  20. *
  21. * Here is where we register a Video JS Component and
  22. * mount the React component to it when the player is ready.
  23. */
  24. import EpisodeList from './EpisodeList';
  25. import ReactDOM from 'react-dom';
  26. import videojs from 'video.js';
  27. const vjsComponent = videojs.getComponent('Component');
  28. class vjsEpisodeList extends vjsComponent {
  29. constructor(player, options) {
  30. super(player, options);
  31. /* Bind the current class context to the mount method */
  32. this.mount = this.mount.bind(this);
  33. /* When player is ready, call method to mount React component */
  34. player.ready(() => {
  35. this.mount();
  36. });
  37. /* Remove React root when component is destroyed */
  38. this.on("dispose", () => {
  39. ReactDOM.unmountComponentAtNode(this.el())
  40. });
  41. }
  42. /**
  43. * We will render out the React EpisodeList component into the DOM element
  44. * generated automatically by the VideoJS createEl() method.
  45. *
  46. * We fetch that generated element using `this.el()`, a method provided by the
  47. * vjsComponent class that this class is extending.
  48. */
  49. mount() {
  50. ReactDOM.render(<EpisodeList vjsComponent={this} body="Episodes" />, this.el() );
  51. }
  52. }
  53. /**
  54. * Make sure to register the vjsComponent so Video JS knows it exists
  55. */
  56. vjsComponent.registerComponent('vjsEpisodeList', vjsEpisodeList);
  57. export default vjsEpisodeList;
  58. /**
  59. * VideoPlayer.js
  60. * Check the above example for how to integrate the rest of this class.
  61. */
  62. // ...
  63. componentDidMount() {
  64. // instantiate Video.js
  65. this.player = videojs(this.videoNode, this.props, function onPlayerReady() {
  66. console.log('onPlayerReady', this)
  67. });
  68. /**
  69. * Fetch the controlBar component and add the new vjsEpisodeList component as a child
  70. * You can pass options here if desired in the second object.
  71. */
  72. this.player.getChild('controlBar').addChild('vjsEpisodeList', {});
  73. }
  74. // ...