Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

video_id for 'pause' event when changing videoId prop is the new one, instead of the previous one #252

Open
JorgeSivil opened this issue Aug 4, 2020 · 1 comment
Labels

Comments

@JorgeSivil
Copy link

JorgeSivil commented Aug 4, 2020

Steps to Reproduce:

Have 1 YouTube component, pass the VideoID.

Have a function that OnClick changes the VideoID to another.

You can see that the "pause" event gets fired for the new video ID instead of the previous video ID.

image

function getPlayer(playerStatus) {
  return (
    <YouTube
      videoId={playerStatus.video.id}
      opts={{
        playerVars: {
          autoplay: 1,
        },
      }}
      onStateChange={(event) => {
        trackPlayerEvent(event.data, event.target.playerInfo.videoData.video_id);
      }}
    />
  );
}

function trackPlayerEvent(eventId, videoId) {
  switch (eventId) {
    // Unstarted
    case -1:
      console.log('YouTube Video Clicked', videoId);
      // trackEvent({
      //   action: 'YouTube Video Clicked',
      //   label: videoId,
      // });
      break;

    // Ended
    case 0:
      console.log('YouTube Video Ended', videoId);
      // trackEvent({
      //   action: 'YouTube Video Ended',
      //   label: videoId,
      // });
      break;

    // Playing
    case 1:
      console.log('YouTube Video Played', videoId);
      // trackEvent({
      //   action: 'YouTube Video Played',
      //   label: videoId,
      // });
      break;

    // Paused
    case 2:
      console.log('YouTube Video Paused', videoId);
      // trackEvent({
      //   action: 'YouTube Video Paused',
      //   label: videoId,
      // });
      break;

    // Buffering
    case 3:
      console.log('YouTube Video Buffered', videoId);
      // trackEvent({
      //   action: 'YouTube Video Buffered',
      //   label: videoId,
      // });
      break;

    case 5:
      // Video Cued
      break;
  }
}
@JorgeSivil JorgeSivil reopened this Aug 4, 2020
@ruisaraiva19
Copy link
Collaborator

@JorgeSivil This behavior is from the YouTube Player API itself on not something manipulated by the react-youtube component code.

@ruisaraiva19 ruisaraiva19 changed the title Video ID for 'pause' event when changing ID prop is the new one, instead of the past one video_id for 'pause' event when changing videoId prop is the new one, instead of the previous one Jan 24, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants