Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

216
Views
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
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!