Skip to content

How to control video player currentTime from a parent component? #1362

Answered by mihar-22
ameytessact asked this question in Q&A
Discussion options

You must be logged in to vote

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>

Replies: 3 comments 2 replies

Comment options

You must be logged in to vote
1 reply
@ameytessact
Comment options

Answer selected by ameytessact
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
1 reply
@mihar-22
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants