Error with using useRef to create ref in functional component
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

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
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.
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)}
/>
);
});
Also just ran into this, glad to see this here. Thank you both!
Thank you @tfomina and @CookPete. Please we make this as a new example with Hook on the Documentation page.