React

React Hooks (Headless)

Build a custom video player UI using PlayerKit's headless React hooks.

React Hooks (Headless)

If you don't want the built-in control bar and settings menus, you can use the headless hooks. These hooks provide the PlayerControls API and playback state management, while leaving the DOM structure and CSS entirely up to you.


useHlsPlayer

Manages an HLS.js instance and binds it to a native <video> element.

Usage

import { useHlsPlayer } from "@playerkit/react";
import type { UseHlsPlayerOptions } from "@playerkit/react";

function CustomHlsPlayer() {
  const { player, state, error, rootRef, videoRef } = useHlsPlayer({
    src: "https://example.com/stream.m3u8",
    autoPlay: true,
  });

  return (
    <div ref={rootRef} className="my-player-root">
      <video ref={videoRef} className="my-video-element" />

      <div className="my-controls">
        <button onClick={() => player?.togglePlay()}>
          {state?.isPlaying ? "Pause" : "Play"}
        </button>
        <span>
          {state?.currentTime?.toFixed(1)} / {state?.duration?.toFixed(1)}
        </span>
      </div>

      {error && <div className="my-error">Error: {error.message}</div>}
    </div>
  );
}

Options

OptionTypeDescription
srcstringURL of the HLS manifest
autoPlaybooleanStart playback automatically
startTimenumberTime to start playback from (seconds)
tokenFetcherTokenFetcherAsync function to fetch auth tokens
tokenRefresherTokenRefresherAsync function to refresh auth tokens
liveLiveConfigHLS live stream configuration
logLevelLogLevelLogger verbosity

Returns

PropertyTypeDescription
playerPlayer | nullThe underlying HLS core engine instance
statePlayerSnapshot | nullReactive snapshot of playback state
errorPlayerError | nullReactive error state
rootRefRefObject<HTMLDivElement>Attach to the outer container (used for fullscreen)
videoRefRefObject<HTMLVideoElement>Attach to the <video> tag

useYoutubePlayer

Manages the YouTube IFrame API. Because YouTube loads inside an <iframe>, you provide a containerRef where the iframe will be injected.

Usage

import { useYoutubePlayer } from "@playerkit/react";
import { useRef } from "react";

function CustomYoutubePlayer() {
  const containerRef = useRef<HTMLDivElement>(null);
  const rootRef = useRef<HTMLDivElement>(null);

  const { player, state, error } = useYoutubePlayer({
    src: "dQw4w9WgXcQ",
    containerRef, // IFrame gets injected here
    fullscreenRef: rootRef, // Used for requestFullscreen
    autoPlay: false,
  });

  return (
    <div ref={rootRef} className="my-youtube-root">
      {/* The YouTube iframe mounts inside this div */}
      <div ref={containerRef} className="my-youtube-iframe-container" />

      <div className="my-controls">
        <button onClick={() => player?.togglePlay()}>
          {state?.isPlaying ? "Pause" : "Play"}
        </button>
      </div>
    </div>
  );
}

Options

OptionTypeDescription
srcstringYouTube URL, video ID, or nocookie embed URL
containerRefRefObject<HTMLElement>Required. Where the iframe is mounted
fullscreenRefRefObject<HTMLElement>Which element to fullscreen
autoPlaybooleanStart playback automatically
startTimenumberTime to start playback from (seconds)
liveLiveConfigLive stream configuration (e.g., DVR)

Returns

PropertyTypeDescription
playerYoutubePlayer | nullThe underlying YouTube core engine
statePlayerSnapshot | nullReactive snapshot of playback state
errorPlayerError | nullReactive error state

useMp4Player

Manages native progressive video playback.

Usage

import { useMp4Player } from "@playerkit/react";

function CustomMp4Player() {
  const { player, state, rootRef, videoRef } = useMp4Player({
    src: "https://example.com/video.mp4",
  });

  return (
    <div ref={rootRef}>
      <video ref={videoRef} />
      <button onClick={() => player?.togglePlay()}>
        {state?.isPlaying ? "Pause" : "Play"}
      </button>
    </div>
  );
}

Options

Accepts the same options as useHlsPlayer, except live (as MP4 does not support live streams).

Returns

Accepts the same return signature as useHlsPlayer, but player is an instance of Mp4Player.

On this page