React YouTube Component
A thin wrapper around the YouTube IFrame API. The goal is to make it easier to use the API in a react centered way. The component creates two HOC wrappers. One that downloads the API and another that creates a player for you. The player is not wrapped in any way so you can use the IFframe API documentation directly on your YouTube player object to load videos, create playlists and show your content.
Control of the player can be managed by one of two ways.
- Parent Element
- Child Element
Installation
Install using npm npm install --save react-youtube-component
;
Use in your react project.
const createYouTube = ;;
Usage
Parent Element
Create your component. Then use it as component. Add your parameters as according to the API specification
const YouTube = ; { superprops; thisplayerVars = autoplay: 0 color: 0 controls: 1 // add as you wish ; } { return <YouTube height="640" width="390" videoId="h_D3VFfhvs4" playerVars=thisplayerVars /> ; }
Interact with your component. Register event callbacks and/or save a reference to the player object so you can call methods directly
const YouTube = ; { superprops; thisplayerVars = autoplay: 0 color: 0 controls: 1 // add as you wish ; } { // do something with the state change event }; { // your player is now ready }; { // save your player reference for later thisplayer = player; }; { // interact with your player using javascript methods if thisplayer thisplayer; }; { return <div> <YouTube height="640" width="390" videoId="h_D3VFfhvs4" playerVars=thisplayerVars onStateChange=thisonStateChange onReady=thisonReady onPlayer=thisonPlayer /> <button onClick=thisonClick>Load Another Video</button> </div> ; }
Child Element
You can also pass in a component to the createYouTube function. This component will have access to the player via props. It will have access to the player events via subscriptions which return an unsubscribe function. Be sure not to pass anything in your render method or it will be overwritten by the <iframe>
.
{ superprops; } { // component receives props from it's parent or redux or somewhere thispropsplayer; } { // do something with state change }; { // video is ready. start playing or do something fun thispropsplayer; }; { thisunsubscribeOnStateChange = thispropsonStateChange; thisunsubscribeOnReady = thispropsonReady; } { this; this; } { return null; // this would be overwritten // return <p>some text</p> } const YouTube = ; // then just use it in your parent { superprops } { return <YouTube />; }
Also be sure not to pass any callbacks via the parent or the similar child functionality will not work. If you pass onPlayer
to the component then this.props.player
will not be available.
Don't Do This
{ superprops } { // This is NOT availble because we passed onPlayer in the parent component below thispropsplayer; } { return null; } const YouTube = ; // elsewhere in parent { superprops } { thisplayer = player; }; { return <YouTube onPlayer=thisonPlayer />; }
Overall this will reinforce a better project structure. You shouldn't be controlling the player from both parent and from within the player as well.