Skip to content

ReferenceText tracks

useCreateTextTrack

Hook that adds a text track to the player while the component is mounted

Import

import { useCreateTextTrack } from '@videojs/react';

useCreateTextTrack adds a text track to the player while the component is mounted, and removes it on unmount. It takes a track definition and the track’s content, or one object describing both. The content holds cues, a src file to load them from, and a format that reads a file in another format. The text cues feature lists every field.

Added through a definition, the cues are checked against its value type.

import { defineTextTrack, useCreateTextTrack, type TextCueInit } from '@videojs/react';

interface Quiz {
  question: string;
}

const quizTrack = defineTextTrack<Quiz>({ kind: 'metadata', label: 'quiz' });

function QuizTrack({ cues }: { cues: TextCueInit<Quiz>[] }) {
  useCreateTextTrack(quizTrack, { cues });

  return null;
}

The hook returns a handle to the track, or null before the track is added. The handle’s addCue(cue) and removeCue(id) change the track’s cues.

Behavior

The hook removes the track when the component unmounts, and replaces it when the track’s kind, label, language, cues, src, or format changes. Keep cues the same array and format the same function across renders, such as by memoizing them or defining them outside the component; a new one replaces the track and drops cues added through the old handle.

The track stays through source changes, so give the component a key per video to swap tracks with the video. Calling the handle’s remove() removes the track until the component remounts or the track is replaced.

Tracks a component adds work with any media, including embeds without text tracks of their own. A captions or subtitles track is copied into the media, so the browser draws it when it’s selected and the captions menu lists it.

API Reference

With Definition

useCreateTextTrack<Cue extends MediaTextCue>(track, content?): TextTrackHandle<InferCueValue<Cue>> | null

Add a track defined with defineTextTrack() while the component is mounted, and remove it when it unmounts. Its cues are checked against the definition's type. The track stays through source changes. Keep cues the same array, and format the same function, across renders; a new one replaces the track.

useCreateTextTrack(quizTrack, { cues });

Parameters

ParameterTypeDefaultDetails
track*object—
contentobject—

Return Value

PropertyTypeDetails
idstring
removedboolean
addCuefunction
removeCuefunction
removefunction

With Description

useCreateTextTrack<Value = unknown>(init): TextTrackHandle<Value> | null

Add a text track while the component is mounted, and remove it when it unmounts. The track stays through source changes. Keep cues the same array, and format the same function, across renders; a new one replaces the track.

useCreateTextTrack({ kind: 'metadata', label: 'quiz', cues });

Parameters

ParameterTypeDefaultDetails
init*object—

Return Value

PropertyTypeDetails
idstring
removedboolean
addCuefunction
removeCuefunction
removefunction