Dashboard

React SDK

The React SDK wraps the underlying Web Component with typed props, ref forwarding, and callback hooks that match React conventions.

Install

npm install @ansdev/player-react

⚠️ Not yet published to npm

The React package ships alongside the v1 stable release of the Player SDK. Until then, drop in the JavaScript SDK via a CDN <script> tag.

Quick start

import { AnsdevPlayer } from '@ansdev/player-react';

export function VideoPage({ videoId, token }: { videoId: string; token: string }) {
  return (
    <AnsdevPlayer
      videoId={videoId}
      playbackToken={token}
      theme={{
        primary: '#22D3EE',
        background: '#0F172A',
      }}
      onPlay={() => console.log('Playing')}
      onEnded={() => console.log('Done')}
    />
  );
}

Props

PropTypeRequiredDescription
videoIdstringyesThe video to play
playbackTokenstringyes*Short-lived JWT from your server. *One of playbackToken or apiKey is required.
apiKeystringnoAPI key — only acceptable in trusted environments. Prefer playbackToken.
themeobjectnoColor tokens (see Customization)
brandobjectno{ name, logo, accent } for integrator branding
controlsobjectnoPer-control visibility toggles
featuresobjectnoFeature flags (keyboardShortcuts, pictureInPicture, etc.)
autoplaybooleannoDefault false. Most browsers require muted={true} for autoplay to work.
mutedbooleannoStart muted
startAtnumbernoInitial playhead in seconds
classNamestringnoWrapper class for layout
styleCSSPropertiesnoInline style on the wrapper

Callback props

PropSignature
onReady(player: PlayerInstance) => void
onPlay() => void
onPause() => void
onSeek(time: number) => void
onEnded() => void
onTimeUpdate(time: number) => void
onQualityChange(quality: string) => void
onError(error: { code: string; message: string }) => void
onProgress(progress: { time: number; duration: number }) => void

TypeScript

Full type definitions ship with the package. No @types/... companion needed.

import type { PlayerInstance, PlayerError } from '@ansdev/player-react';

SSR (Next.js)

The player is client-side only. Disable SSR with a dynamic import:

import dynamic from 'next/dynamic';

const AnsdevPlayer = dynamic(
  () => import('@ansdev/player-react').then((m) => m.AnsdevPlayer),
  { ssr: false },
);

Refs

Forwarded ref returns a PlayerInstance with the same imperative methods as the JavaScript SDK:

import { useRef } from 'react';
import { AnsdevPlayer, type PlayerInstance } from '@ansdev/player-react';

export function ControlledPlayer() {
  const ref = useRef<PlayerInstance>(null);
  return (
    <>
      <AnsdevPlayer ref={ref} videoId="vid_abc123def456" playbackToken="pt_..." />
      <button type="button" onClick={() => ref.current?.play()}>Play</button>
    </>
  );
}

See also: Events · Customization · Keyboard Shortcuts.