How to control video player currentTime from a parent component? #1362
-
I have a timeline component, from where, I want to seek the video. I know that it's possible to seek using Currently, I am using a state that does the job as follows: const [currentTime, setCurrentTime] = useState<number>(0); <MediaPlayer currentTime={currentTime}
setCurrentTime={setCurrentTime}>
<MediaProvider className="flex h-full w-full items-center justify-center bg-ds-videoplayer-bg [&>video]:h-full" />
<VideoLayout />
</MediaPlayer> What is the best practice to do this? const playerCurrentTime = useMediaState('currentTime', player);
console.log(playerCurrentTime) It is equal to |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 2 replies
-
If you need to pull state up, pass down a callback via context: import type { MediaPlayerInstance } from '@vidstack/react';
export interface PlayerContextValue {
setPlayer(player: MediaPlayerInstance | null): void;
}
export const PlayerContext = createContext<PlayerContextValue>(); In parent: const [player, setPlayer] = useState<MediaPlayerInstance | null>(null);
useEffect(() => {
// ...
}, [player])
<PlayerContext.Provider value={{ setPlayer }}>
...
</PlayerContext.Provider> In child: const { setPlayer } = useContext(PlayerContext);
<MediaPlayer ref={setPlayer}>
...
</MediaPlayer> |
Beta Was this translation helpful? Give feedback.
-
I'll look into this. |
Beta Was this translation helpful? Give feedback.
-
@mihar-22 Is it possible to override the amount of time on arrow left and right keys without passing custom |
Beta Was this translation helpful? Give feedback.
If you need to pull state up, pass down a callback via context:
In parent:
In child: