1
1
import React , {
2
2
useRef ,
3
3
useMemo ,
4
+ useState ,
4
5
useEffect ,
5
6
forwardRef ,
6
7
useCallback ,
@@ -50,7 +51,7 @@ const YoutubeIframe = (props, ref) => {
50
51
onPlaybackRateChange = _playbackRate => { } ,
51
52
} = props ;
52
53
53
- const playerReady = useRef ( false ) ;
54
+ const [ playerReady , setPlayerReady ] = useState ( false ) ;
54
55
const lastVideoIdRef = useRef ( videoId ) ;
55
56
const lastPlayListRef = useRef ( playList ) ;
56
57
const initialPlayerParamsRef = useRef ( initialPlayerParams || { } ) ;
@@ -117,7 +118,7 @@ const YoutubeIframe = (props, ref) => {
117
118
) ;
118
119
119
120
useEffect ( ( ) => {
120
- if ( ! playerReady . current ) {
121
+ if ( ! playerReady ) {
121
122
// no instance of player is ready
122
123
return ;
123
124
}
@@ -128,10 +129,10 @@ const YoutubeIframe = (props, ref) => {
128
129
PLAYER_FUNCTIONS . setVolume ( volume ) ,
129
130
PLAYER_FUNCTIONS . setPlaybackRate ( playbackRate ) ,
130
131
] . forEach ( webViewRef . current . injectJavaScript ) ;
131
- } , [ play , mute , volume , playbackRate ] ) ;
132
+ } , [ play , mute , volume , playbackRate , playerReady ] ) ;
132
133
133
134
useEffect ( ( ) => {
134
- if ( ! playerReady . current || lastVideoIdRef . current === videoId ) {
135
+ if ( ! playerReady || lastVideoIdRef . current === videoId ) {
135
136
// no instance of player is ready
136
137
// or videoId has not changed
137
138
return ;
@@ -142,10 +143,10 @@ const YoutubeIframe = (props, ref) => {
142
143
webViewRef . current . injectJavaScript (
143
144
PLAYER_FUNCTIONS . loadVideoById ( videoId , play ) ,
144
145
) ;
145
- } , [ videoId , play ] ) ;
146
+ } , [ videoId , play , playerReady ] ) ;
146
147
147
148
useEffect ( ( ) => {
148
- if ( ! playerReady . current ) {
149
+ if ( ! playerReady ) {
149
150
// no instance of player is ready
150
151
return ;
151
152
}
@@ -161,7 +162,7 @@ const YoutubeIframe = (props, ref) => {
161
162
webViewRef . current . injectJavaScript (
162
163
PLAYER_FUNCTIONS . loadPlaylist ( playList , playListStartIndex , play ) ,
163
164
) ;
164
- } , [ playList , play , playListStartIndex ] ) ;
165
+ } , [ playList , play , playListStartIndex , playerReady ] ) ;
165
166
166
167
const onWebMessage = useCallback (
167
168
event => {
@@ -177,7 +178,7 @@ const YoutubeIframe = (props, ref) => {
177
178
break ;
178
179
case 'playerReady' :
179
180
onReady ( ) ;
180
- playerReady . current = true ;
181
+ setPlayerReady ( true ) ;
181
182
break ;
182
183
case 'playerQualityChange' :
183
184
onPlaybackQualityChange ( message . data ) ;
0 commit comments