react-player icon indicating copy to clipboard operation
react-player copied to clipboard

Error with using useRef to create ref in functional component

Open tfomina opened this issue 6 years ago • 4 comments

Current Behavior

Hi! I would like to use ReactPlayer in my functional component and use useRef to create ref to ReactPlayer

import React, { useState, useEffect, useRef } from "react";
import ReactPlayer from "react-player";
import { useSelector } from "../../../redux/useSelector";
import { msToSeconds } from "../../../utils";

const VIDEO_URL =
  "http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4";

export const Player: React.FC = React.memo(() => {
  const player = useRef<any>(null);
  const [duration, setDuration] = useState<number>(0);

  const targetTimestamp = useSelector((state) => {
    const {
      timestamps: { selectedTimestamp },
    } = state;
    return (selectedTimestamp && selectedTimestamp.timestamp) || 0;
  });

  useEffect(() => {
    console.log("targetTimestamp ", targetTimestamp);
    const targetTimestampInSeconds = msToSeconds(targetTimestamp);

    if (targetTimestampInSeconds > duration) return;

    player.current &&
      player.current.seekTo(targetTimestampInSeconds, "seconds");
  }, [targetTimestamp]);

  const handleDuration = (duration: number) => {
    setDuration(duration);
  };

  return (
    <ReactPlayer
      ref={player}
      url={VIDEO_URL}
      controls
      width={1280}
      height={720}
      playing
      onDuration={handleDuration}
    />
  );
});

console.log(player.current); works, but on the next line I get the error

Uncaught TypeError: Cannot read property 'seekTo' of undefined

Screenshot

What's wrong? Is it impossible to use ReactPlayer inside functional component and I could use it only in class component? Could you give me a hint, please?

Environment

My project https://github.com/tfomina/Video-App

tfomina avatar Jun 07 '20 18:06 tfomina

It looks like you are trying to call seekTo before the player is ready. Use the onReady callback to know when seekTo should be available to call.

cookpete avatar Jun 07 '20 19:06 cookpete

Oh, thank you! I added onReady and some checks in my component and now it works perfectly fine. My approach

import React, { useState, useEffect, useRef } from "react";
import ReactPlayer from "react-player";
import { useSelector } from "../../../redux/useSelector";
import { msToSeconds } from "../../../utils";

const VIDEO_URL =
  "http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4";

export const Player: React.FC = React.memo(() => {
  const player = useRef<any>(null);
  const [duration, setDuration] = useState<number>(0);
  const [isReady, setIsReady] = useState<boolean>(false);

  const targetTimestamp = useSelector((state) => {
    const {
      timestamps: { selectedTimestamp },
    } = state;
    return selectedTimestamp && selectedTimestamp.timestamp;
  });

  useEffect(() => {
    if (!targetTimestamp) return;

    const targetTimestampInSeconds = msToSeconds(targetTimestamp);

    if (targetTimestampInSeconds > duration) return;

    isReady &&
      player.current &&
      player.current.seekTo(targetTimestampInSeconds, "seconds");
  }, [targetTimestamp]);

  const handleDuration = (duration: number) => {
    setDuration(duration);
  };

  return (
    <ReactPlayer
      ref={player}
      url={VIDEO_URL}
      controls
      width={1280}
      height={720}
      onDuration={handleDuration}
      onReady={() => setIsReady(true)}
    />
  );
});

tfomina avatar Jun 07 '20 20:06 tfomina

Also just ran into this, glad to see this here. Thank you both!

chrislgrigg avatar Jul 07 '20 00:07 chrislgrigg

Thank you @tfomina and @CookPete. Please we make this as a new example with Hook on the Documentation page.

ntt2k avatar Oct 15 '20 18:10 ntt2k