Simple React component acting as a thin layer over the YouTube IFrame Player API
$ npm install react-youtube<YouTubevideoId={string}// defaults -> nullid={string}// defaults -> nullclassName={string}// defaults -> nullcontainerClassName={string}// defaults -> ''opts={obj}// defaults -> {}onReady={func}// defaults -> nooponPlay={func}// defaults -> nooponPause={func}// defaults -> nooponEnd={func}// defaults -> nooponError={func}// defaults -> nooponStateChange={func}// defaults -> nooponPlaybackRateChange={func}// defaults -> nooponPlaybackQualityChange={func}// defaults -> noop/>For convenience it is also possible to access the PlayerState constants through react-youtube:
YouTube.PlayerState contains the values that are used by the YouTube IFrame Player API.
importReactfrom'react';importYouTubefrom'react-youtube';classExampleextendsReact.Component{render(){constopts={height: '390',width: '640',playerVars: {// https://developers.google.com/youtube/player_parametersautoplay: 1,},};return<YouTubevideoId="2g811Eo7K8U"opts={opts}onReady={this._onReady}/>;}_onReady(event){// access to player in all event handlers via event.targetevent.target.pauseVideo();}}You can access & control the player in a way similar to the official api:
The
APIcomponent will pass an event object as the sole argument to each ofthose functionsthe event handler props. The event object has the following properties:
- The event's
targetidentifies the video player that corresponds to the event.- The event's
dataspecifies a value relevant to the event. Note that theonReadyevent does not specify adataproperty.
MIT