-
Notifications
You must be signed in to change notification settings - Fork 7.5k
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
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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); | ||
} | ||
}, [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, | ||
|
@@ -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} /> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍🏻 |
||
|
||
<div>Rest of app here</div> | ||
</> | ||
); | ||
} | ||
|
||
``` | ||
|
||
## React Class Component Example | ||
|
@@ -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> | ||
|
@@ -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 | ||
|
||
|
@@ -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()) | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍🏻