Skip to content

Commit ad78661

Browse files
committed
fix: fix autoplay behaviour
playerReady was made into a ref that caused this issue. reverting it to a stateful variable triggeres the play/pause scripts as expected fix #204
1 parent e8dfb39 commit ad78661

File tree

1 file changed

+9
-8
lines changed

1 file changed

+9
-8
lines changed

src/YoutubeIframe.js

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import React, {
22
useRef,
33
useMemo,
4+
useState,
45
useEffect,
56
forwardRef,
67
useCallback,
@@ -50,7 +51,7 @@ const YoutubeIframe = (props, ref) => {
5051
onPlaybackRateChange = _playbackRate => {},
5152
} = props;
5253

53-
const playerReady = useRef(false);
54+
const [playerReady, setPlayerReady] = useState(false);
5455
const lastVideoIdRef = useRef(videoId);
5556
const lastPlayListRef = useRef(playList);
5657
const initialPlayerParamsRef = useRef(initialPlayerParams || {});
@@ -117,7 +118,7 @@ const YoutubeIframe = (props, ref) => {
117118
);
118119

119120
useEffect(() => {
120-
if (!playerReady.current) {
121+
if (!playerReady) {
121122
// no instance of player is ready
122123
return;
123124
}
@@ -128,10 +129,10 @@ const YoutubeIframe = (props, ref) => {
128129
PLAYER_FUNCTIONS.setVolume(volume),
129130
PLAYER_FUNCTIONS.setPlaybackRate(playbackRate),
130131
].forEach(webViewRef.current.injectJavaScript);
131-
}, [play, mute, volume, playbackRate]);
132+
}, [play, mute, volume, playbackRate, playerReady]);
132133

133134
useEffect(() => {
134-
if (!playerReady.current || lastVideoIdRef.current === videoId) {
135+
if (!playerReady || lastVideoIdRef.current === videoId) {
135136
// no instance of player is ready
136137
// or videoId has not changed
137138
return;
@@ -142,10 +143,10 @@ const YoutubeIframe = (props, ref) => {
142143
webViewRef.current.injectJavaScript(
143144
PLAYER_FUNCTIONS.loadVideoById(videoId, play),
144145
);
145-
}, [videoId, play]);
146+
}, [videoId, play, playerReady]);
146147

147148
useEffect(() => {
148-
if (!playerReady.current) {
149+
if (!playerReady) {
149150
// no instance of player is ready
150151
return;
151152
}
@@ -161,7 +162,7 @@ const YoutubeIframe = (props, ref) => {
161162
webViewRef.current.injectJavaScript(
162163
PLAYER_FUNCTIONS.loadPlaylist(playList, playListStartIndex, play),
163164
);
164-
}, [playList, play, playListStartIndex]);
165+
}, [playList, play, playListStartIndex, playerReady]);
165166

166167
const onWebMessage = useCallback(
167168
event => {
@@ -177,7 +178,7 @@ const YoutubeIframe = (props, ref) => {
177178
break;
178179
case 'playerReady':
179180
onReady();
180-
playerReady.current = true;
181+
setPlayerReady(true);
181182
break;
182183
case 'playerQualityChange':
183184
onPlaybackQualityChange(message.data);

0 commit comments

Comments
 (0)