Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs(react): update react functional component tutorial #7377

Merged
merged 2 commits into from
Aug 23, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
89 changes: 61 additions & 28 deletions docs/guides/react.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,43 +12,55 @@ import "video.js/dist/video-js.css";
export const VideoJS = ( props ) => {

const videoRef = React.useRef(null);
const { options } = props;
const playerRef = React.useRef(null);
const { options, onReady } = props;

// This separate functional component fixes the removal of the videoelement
// from the DOM when calling the dispose() method on a player
const VideoHtml = ( props ) => (
<div data-vjs-player>
<video ref={videoRef} className="video-js vjs-big-play-centered" />
</div>
);
React.useEffect(() => {
// make sure Video.js player is only initialized once
if (!playerRef.current) {
const videoElement = videoRef.current;
if (!videoElement) return;

React.useEffect( () => {
const videoElement = videoRef.current;
let player;
if( videoElement ) {
player = videojs( videoElement, options, () => {
const player = playerRef.current = videojs(videoElement, options, () => {
console.log("player is ready");
onReady && onReady(player);
});
} else {
// you can update player here [update player through props]
// const player = playerRef.current;
// player.autoplay(options.autoplay);
// player.src(options.sources);
Comment on lines +29 to +32
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍🏻

}
}, [options]);

// Dispose the Video.js player when the functional component unmounts
React.useEffect(() => {
return () => {
if( player ) {
player.dispose();
if (playerRef.current) {
playerRef.current.dispose();
playerRef.current = null;
}
}
}, [options]);
};
}, []);

return (<VideoHtml />);
return (
<div data-vjs-player>
<video ref={videoRef} className="video-js vjs-big-play-centered" />
</div>
);
}
export default VideoJS;

export default VideoJS;
```

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

```jsx
import React from "react";
import VideoJS from './VideoJS' // point to where the functional component is stored

const App = () => {
const playerRef = React.useRef(null);

const videoJsOptions = { // lookup the options in the docs for more options
autoplay: true,
Expand All @@ -60,18 +72,40 @@ const App = () => {
type: 'video/mp4'
}]
}


const handlePlayerReady = (player) => {
playerRef.current = player;

// you can handle player events here
player.on('waiting', () => {
console.log('player is waiting');
});

player.on('dispose', () => {
console.log('player will dispose');
});
};

// const changePlayerOptions = () => {
// // you can update the player through the Video.js player instance
// if (!playerRef.current) {
// return;
// }
// // [update player through instance's api]
// playerRef.current.src([{src: 'http://ex.com/video.mp4', type: 'video/mp4'}]);
// playerRef.current.autoplay(false);
// };

return (
<>
<div>Rest of app here</div>
<VideoJS options={videoJsOptions}/>

<VideoJS options={videoJsOptions} onReady={handlePlayerReady} />
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍🏻


<div>Rest of app here</div>
</>
);
}

```

## React Class Component Example
Expand Down Expand Up @@ -103,7 +137,7 @@ export default class VideoPlayer extends React.Component {
// see https://github.com/videojs/video.js/pull/3856
render() {
return (
<div>
<div>
<div data-vjs-player>
<video ref={ node => this.videoNode = node } className="video-js"></video>
</div>
Expand All @@ -127,9 +161,8 @@ const videoJsOptions = {

return <VideoPlayer { ...videoJsOptions } />
```
[options]: /docs/guides/options.md


[options]: /docs/guides/options.md

## Using a React Component as a Video JS Component

Expand Down Expand Up @@ -178,7 +211,7 @@ class vjsEpisodeList extends vjsComponent {
player.ready(() => {
this.mount();
});

/* Remove React root when component is destroyed */
this.on("dispose", () => {
ReactDOM.unmountComponentAtNode(this.el())
Expand Down