Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

228
Visualizações
onYouTubeIframeAPIReady event is not triggering in second tab but works fine in first tab

I tried to load youtube videos under 3 tabs in a page. In the initial load of the react component, the video under first page works well. If navigated to the second or third tab, the youtube player is not mounting in the DOM. What i understood from this behavior during the tab switch is, the 'onYouTubeIframeAPIReady' event is not triggering from the downloaded script after switching the tabs. I ensured that on each tab switch the component is rendered and new script is downloaded from the iFrame api.

import {
  Component
} from 'react';

class VideoPlayer extends Component {
  componentDidMount() {
    const id = 'youtube-id';
    if (!document.getElementById(id)) {
      const tag = document.createElement('script');
      tag.setAttribute('src', 'https://www.youtube.com/iframe_api');
      tag.setAttribute('id', id);
      document.body.appendChild(tag);
      window.youtubeFrames = [];
      window.onYouTubeIframeAPIReady = this.loadYoutubeVideo;
    }
    window.youtubeFrames.push({
      video: this.video,
      videoId: this.props.Id,
    });
  }

  componentWillUnmount() {
    const elem = document.getElementById('youtube-id');
    if (elem) {
      elem.parentNode.removeChild(elem);
    }
  }

  loadYoutubeVideo = () => {
    let player;
    window.youtubeFrames.forEach((youtubeFrame) => {
      if (youtubeFrame.video) {
        player = new YT.Player(youtubeFrame.video, {
          height: `${this.video.style.height}px`,
          width: '100%',
          videoId: youtubeFrame.videoId,
          playerVars: {
            rel: 0,
          },
          events: {
            onStateChange: (e) => {
              if (e.data === 0) {
                youtubeFrame.coverFrame.classList.remove('play');
              } else {
                youtubeFrame.coverFrame.classList.add('play');
              }
            },
          },
        });
      }
    });
    /* eslint-enable */
  }
  render() {
    const videoRef = arg => (this.video = arg);

    return ( <
      div className = "youtube-video"
      ref = {
        coverRef
      } >
      <
      div className = "video"
      ref = {
        videoRef
      }
      /> < /
      div >
    );
  };
}

export default VideoPlayer;

about 4 years ago · Juan Pablo Isaza
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda