@@ -55,6 +55,11 @@ const YoutubeIframe = (props, ref) => {
5555 const webViewRef = useRef ( null ) ;
5656 const eventEmitter = useRef ( new EventEmitter ( ) ) ;
5757
58+ const sendPostMessage = useCallback ( ( eventName , meta ) => {
59+ const message = JSON . stringify ( { eventName, meta} ) ;
60+ webViewRef . current . postMessage ( message ) ;
61+ } , [ ] ) ;
62+
5863 useImperativeHandle (
5964 ref ,
6065 ( ) => ( {
@@ -119,17 +124,25 @@ const YoutubeIframe = (props, ref) => {
119124 return ;
120125 }
121126
122- webViewRef . current . postMessage ( play ? 'playVideo' : 'pauseVideo' ) ;
123- } , [ play , playerReady ] ) ;
127+ if ( play ) {
128+ sendPostMessage ( 'playVideo' , { } ) ;
129+ } else {
130+ sendPostMessage ( 'pauseVideo' , { } ) ;
131+ }
132+ } , [ play , playerReady , sendPostMessage ] ) ;
124133
125134 useEffect ( ( ) => {
126135 if ( ! playerReady ) {
127136 // no instance of player is ready
128137 return ;
129138 }
130139
131- webViewRef . current . postMessage ( mute ? 'muteVideo' : 'unMuteVideo' ) ;
132- } , [ mute , playerReady ] ) ;
140+ if ( mute ) {
141+ sendPostMessage ( 'muteVideo' , { } ) ;
142+ } else {
143+ sendPostMessage ( 'unMuteVideo' , { } ) ;
144+ }
145+ } , [ mute , playerReady , sendPostMessage ] ) ;
133146
134147 useEffect ( ( ) => {
135148 if ( ! playerReady ) {
0 commit comments