Skip to content

🎥 Make videos programmatically with React

Docs: Update examples to React 19 Ref TypeScript types

Jonny Burger3324e9ee455b
diff --git a/packages/docs/docs/player/best-practices.mdx b/packages/docs/docs/player/best-practices.mdx
index 4d2754ed9b..58d1cb8735 100644
--- a/packages/docs/docs/player/best-practices.mdx
+++ b/packages/docs/docs/player/best-practices.mdx
@@ -1,10 +1,10 @@
---
image: /generated/articles-docs-player-best-practices.png
id: best-practices
-sidebar_label: "Best practices"
-title: "Player - Best practices"
+sidebar_label: 'Best practices'
+title: 'Player - Best practices'
slug: /player/best-practices
-crumb: "@remotion/player"
+crumb: '@remotion/player'
---
## Avoid re-renders of the `<Player>`
@@ -23,9 +23,9 @@ const otherProps = {
compositionHeight: 1080,
fps: 30,
} as const;
-import { Player, PlayerRef } from "@remotion/player";
-import { useEffect, useRef, useState } from "react";
-import { MyVideo } from "./remotion/MyVideo";
+import {Player, PlayerRef} from '@remotion/player';
+import {useEffect, useRef, useState} from 'react';
+import {MyVideo} from './remotion/MyVideo';
// ---cut---
export const App: React.FC = () => {
@@ -33,7 +33,7 @@ export const App: React.FC = () => {
const [currentTime, setCurrentTime] = useState(0);
useEffect(() => {
- playerRef.current?.addEventListener("timeupdate", (e) => {
+ playerRef.current?.addEventListener('timeupdate', (e) => {
setCurrentTime(e.detail.frame);
});
}, []);
@@ -61,24 +61,24 @@ const otherProps = {
compositionHeight: 1080,
fps: 30,
} as const;
-import { Player, PlayerRef } from "@remotion/player";
-import { useEffect, useRef, useState } from "react";
-import { MyVideo } from "./remotion/MyVideo";
+import {Player, PlayerRef} from '@remotion/player';
+import {useEffect, useRef, useState} from 'react';
+import {MyVideo} from './remotion/MyVideo';
// ---cut---
const PlayerOnly: React.FC<{
- playerRef: React.RefObject<PlayerRef>;
-}> = ({ playerRef }) => {
+ playerRef: React.RefObject<PlayerRef | null>;
+}> = ({playerRef}) => {
return <Player ref={playerRef} component={MyVideo} {...otherProps} />;
};
const ControlsOnly: React.FC<{
- playerRef: React.RefObject<PlayerRef>;
-}> = ({ playerRef }) => {
+ playerRef: React.RefObject<PlayerRef | null>;
+}> = ({playerRef}) => {
const [currentTime, setCurrentTime] = useState(0);
useEffect(() => {
- playerRef.current?.addEventListener("timeupdate", (e) => {
+ playerRef.current?.addEventListener('timeupdate', (e) => {
setCurrentTime(e.detail.frame);
});
}, []);
@@ -122,12 +122,12 @@ export const MyVideo = () => <></>;
// @filename: index.tsx
// ---cut---
-import { Player } from "@remotion/player";
-import { useState, useMemo } from "react";
-import { MyVideo } from "./remotion/MyVideo";
+import {Player} from '@remotion/player';
+import {useState, useMemo} from 'react';
+import {MyVideo} from './remotion/MyVideo';
export const App: React.FC = () => {
- const [text, setText] = useState("world");
+ const [text, setText] = useState('world');
const inputProps = useMemo(() => {
return {
text,
diff --git a/packages/docs/docs/player/current-time.mdx b/packages/docs/docs/player/current-time.mdx
index 1afc9dd07a..1a658c8943 100644
--- a/packages/docs/docs/player/current-time.mdx
+++ b/packages/docs/docs/player/current-time.mdx
@@ -1,10 +1,10 @@
---
image: /generated/articles-docs-player-current-time.png
id: current-time
-sidebar_label: "Getting the current time"
-title: "Displaying the current time"
+sidebar_label: 'Getting the current time'
+title: 'Displaying the current time'
slug: /player/current-time
-crumb: "@remotion/player"
+crumb: '@remotion/player'
---
When rendering a [`<Player>`](/docs/player) in your app, special considerations must be taken to prevent constant re-renders of the app or [`<Player>`](/docs/player) if the time changes.
@@ -20,31 +20,33 @@ Do not put this hook into the same component in which the `<Player>` is rendered
If you want to display a component that synchronizes with the time of the player, for example the cursor of a timeline component or a custom time display, you can use the following hook:
```tsx twoslash title="use-current-player-frame.ts"
-import { CallbackListener, PlayerRef } from "@remotion/player";
-import { useCallback, useSyncExternalStore } from "react";
+import {CallbackListener, PlayerRef} from '@remotion/player';
+import {useCallback, useSyncExternalStore} from 'react';
-export const useCurrentPlayerFrame = (ref: React.RefObject<PlayerRef>) => {
+export const useCurrentPlayerFrame = (
+ ref: React.RefObject<PlayerRef | null>,
+) => {
const subscribe = useCallback(
(onStoreChange: () => void) => {
- const { current } = ref;
+ const {current} = ref;
if (!current) {
return () => undefined;
}
- const updater: CallbackListener<"frameupdate"> = ({ detail }) => {
+ const updater: CallbackListener<'frameupdate'> = ({detail}) => {
onStoreChange();
};
- current.addEventListener("frameupdate", updater);
+ current.addEventListener('frameupdate', updater);
return () => {
- current.removeEventListener("frameupdate", updater);
+ current.removeEventListener('frameupdate', updater);
};
},
- [ref]
+ [ref],
);
const data = useSyncExternalStore<number>(
subscribe,
() => ref.current?.getCurrentFrame() ?? 0,
- () => 0
+ () => 0,
);
return data;
@@ -61,15 +63,15 @@ Add a ref to a React Player and pass it to another component:
export const MyVideo = () => <></>;
// @filename: ./remotion/TimeDisplay.tsx
export const TimeDisplay: React.FC<{
- playerRef: React.RefObject<PlayerRef>;
+ playerRef: React.RefObject<PlayerRef | null>;
}> = () => <></>;
// @filename: index.tsx
// ---cut---
-import { Player, PlayerRef } from "@remotion/player";
-import { useRef } from "react";
-import { MyVideo } from "./remotion/MyVideo";
-import { TimeDisplay } from "./remotion/TimeDisplay";
+import {Player, PlayerRef} from '@remotion/player';
+import {useRef} from 'react';
+import {MyVideo} from './remotion/MyVideo';
+import {TimeDisplay} from './remotion/TimeDisplay';
export const App: React.FC = () => {
const playerRef = useRef<PlayerRef>(null);
@@ -94,31 +96,33 @@ This is how a component could access the current time:
```tsx twoslash title="TimeDisplay.tsx"
// @filename: ./use-current-player-frame.ts
-import { CallbackListener, PlayerRef } from "@remotion/player";
-import { useCallback, useSyncExternalStore } from "react";
+import {CallbackListener, PlayerRef} from '@remotion/player';
+import {useCallback, useSyncExternalStore} from 'react';
-export const useCurrentPlayerFrame = (ref: React.RefObject<PlayerRef>) => {
+export const useCurrentPlayerFrame = (
+ ref: React.RefObject<PlayerRef | null>,
+) => {
const subscribe = useCallback(
(onStoreChange: (newVal: number) => void) => {
- const { current } = ref;
+ const {current} = ref;
if (!current) {
return () => undefined;
}
- const updater: CallbackListener<"frameupdate"> = ({ detail }) => {
+ const updater: CallbackListener<'frameupdate'> = ({detail}) => {
onStoreChange(detail.frame);
};
- current.addEventListener("frameupdate", updater);
+ current.addEventListener('frameupdate', updater);
return () => {
- current.removeEventListener("frameupdate", updater);
+ current.removeEventListener('frameupdate', updater);
};
},
- [ref]
+ [ref],
);
const data = useSyncExternalStore<number>(
subscribe,
() => ref.current?.getCurrentFrame() ?? 0,
- () => 0
+ () => 0,
);
return data;
@@ -126,13 +130,13 @@ export const useCurrentPlayerFrame = (ref: React.RefObject<PlayerRef>) => {
// @filename: TimeDisplay.tsx
// ---cut---
-import React from "react";
-import { PlayerRef } from "@remotion/player";
-import { useCurrentPlayerFrame } from "./use-current-player-frame";
+import React from 'react';
+import {PlayerRef} from '@remotion/player';
+import {useCurrentPlayerFrame} from './use-current-player-frame';
export const TimeDisplay: React.FC<{
- playerRef: React.RefObject<PlayerRef>;
-}> = ({ playerRef }) => {
+ playerRef: React.RefObject<PlayerRef | null>;
+}> = ({playerRef}) => {
const frame = useCurrentPlayerFrame(playerRef);
return <div>current frame: {frame}</div>;
diff --git a/packages/docs/docs/player/custom-controls.mdx b/packages/docs/docs/player/custom-controls.mdx
index 3f5402e58d..a7e363e0e1 100644
--- a/packages/docs/docs/player/custom-controls.mdx
+++ b/packages/docs/docs/player/custom-controls.mdx
@@ -49,7 +49,7 @@ import type {PlayerRef} from '@remotion/player';
import {useCallback, useEffect, useState} from 'react';
export const PlayPauseButton: React.FC<{
- playerRef: React.RefObject<PlayerRef>;
+ playerRef: React.RefObject<PlayerRef | null>;
}> = ({playerRef}) => {
const [playing, setPlaying] = useState(false);
@@ -123,7 +123,7 @@ export const formatTime = (frame: number, fps: number): string => {
export const TimeDisplay: React.FC<{
durationInFrames: number;
fps: number;
- playerRef: React.RefObject<PlayerRef>;
+ playerRef: React.RefObject<PlayerRef | null>;
}> = ({durationInFrames, fps, playerRef}) => {
const [time, setTime] = React.useState(0);
@@ -174,7 +174,7 @@ import type {PlayerRef} from '@remotion/player';
import React, {useCallback, useEffect, useState} from 'react';
export const FullscreenButton: React.FC<{
- playerRef: React.RefObject<PlayerRef>;
+ playerRef: React.RefObject<PlayerRef | null>;
}> = ({playerRef}) => {
const [supportsFullscreen, setSupportsFullscreen] = useState(false);
const [isFullscreen, setIsFullscreen] = useState(false);
@@ -256,7 +256,7 @@ type Size = {
// force an update
export const useElementSize = (
- ref: React.RefObject<HTMLElement>,
+ ref: React.RefObject<HTMLElement | null>,
): Size | null => {
const [size, setSize] = useState<Size | null>(() => {
if (!ref.current) {
@@ -420,7 +420,7 @@ const findBodyInWhichDivIsLocated = (div: HTMLElement) => {
return current;
};
-export const useHoverState = (ref: React.RefObject<HTMLDivElement>) => {
+export const useHoverState = (ref: React.RefObject<HTMLDivElement | null>) => {
const [hovered, setHovered] = useState(false);
useEffect(() => {
@@ -458,7 +458,7 @@ export const SeekBar: React.FC<{
durationInFrames: number;
inFrame?: number | null;
outFrame?: number | null;
- playerRef: React.RefObject<PlayerRef>;
+ playerRef: React.RefObject<PlayerRef | null>;
}> = ({durationInFrames, inFrame, outFrame, playerRef}) => {
const containerRef = useRef<HTMLDivElement>(null);
const barHovered = useHoverState(containerRef);
@@ -757,7 +757,7 @@ import type {PlayerRef} from '@remotion/player';
import React, {useEffect, useState} from 'react';
export const VolumeSlider: React.FC<{
- playerRef: React.RefObject<PlayerRef>;
+ playerRef: React.RefObject<PlayerRef | null>;
}> = ({playerRef}) => {
const [volume, setVolume] = useState(playerRef.current?.getVolume() ?? 1);
const [muted, setMuted] = useState(playerRef.current?.isMuted() ?? false);
@@ -825,7 +825,7 @@ import type {PlayerRef} from '@remotion/player';
import React, {useEffect, useState} from 'react';
export const MuteButton: React.FC<{
- playerRef: React.RefObject<PlayerRef>;
+ playerRef: React.RefObject<PlayerRef | null>;
}> = ({playerRef}) => {
const [muted, setMuted] = useState(playerRef.current?.isMuted() ?? false);
diff --git a/packages/docs/docs/use-offthread-video-texture.mdx b/packages/docs/docs/use-offthread-video-texture.mdx
index 0634113804..804a284b4e 100644
--- a/packages/docs/docs/use-offthread-video-texture.mdx
+++ b/packages/docs/docs/use-offthread-video-texture.mdx
@@ -2,7 +2,7 @@
image: /generated/articles-docs-use-offthread-video-texture.png
id: use-offthread-video-texture
title: useOffthreadVideoTexture()
-crumb: "@remotion/three"
+crumb: '@remotion/three'
---
_available from v4.0.83_
@@ -19,15 +19,15 @@ The return type of it is a `THREE.Texture | null` which you can assign as a `map
## Example
```tsx twoslash title="Simple usage (only works during rendering)"
-import { ThreeCanvas, useOffthreadVideoTexture } from "@remotion/three";
-import { staticFile, useVideoConfig } from "remotion";
+import {ThreeCanvas, useOffthreadVideoTexture} from '@remotion/three';
+import {staticFile, useVideoConfig} from 'remotion';
-const videoSrc = staticFile("/vid.mp4");
+const videoSrc = staticFile('/vid.mp4');
const My3DVideo = () => {
- const { width, height } = useVideoConfig();
+ const {width, height} = useVideoConfig();
- const videoTexture = useOffthreadVideoTexture({ src: videoSrc });
+ const videoTexture = useOffthreadVideoTexture({src: videoSrc});
return (
<ThreeCanvas width={width} height={height}>
@@ -44,24 +44,24 @@ import {
ThreeCanvas,
useOffthreadVideoTexture,
useVideoTexture,
-} from "@remotion/three";
-import { useRef } from "react";
+} from '@remotion/three';
+import {useRef} from 'react';
import {
getRemotionEnvironment,
staticFile,
useVideoConfig,
Video,
-} from "remotion";
+} from 'remotion';
-const videoSrc = staticFile("/vid.mp4");
+const videoSrc = staticFile('/vid.mp4');
const useVideoOrOffthreadVideoTexture = (
videoSrc: string,
- videoRef: React.RefObject<HTMLVideoElement>,
+ videoRef: React.RefObject<HTMLVideoElement | null>,
) => {
if (getRemotionEnvironment().isRendering) {
// eslint-disable-next-line react-hooks/rules-of-hooks
- return useOffthreadVideoTexture({ src: videoSrc });
+ return useOffthreadVideoTexture({src: videoSrc});
}
// eslint-disable-next-line react-hooks/rules-of-hooks
@@ -69,7 +69,7 @@ const useVideoOrOffthreadVideoTexture = (
};
const My3DVideo = () => {
- const { width, height } = useVideoConfig();
+ const {width, height} = useVideoConfig();
const videoRef = useRef<HTMLVideoElement | null>(null);
const videoTexture = useVideoOrOffthreadVideoTexture(videoSrc, videoRef);
@@ -80,7 +80,7 @@ const My3DVideo = () => {
<Video
ref={videoRef}
src={videoSrc}
- style={{ position: "absolute", opacity: 0 }}
+ style={{position: 'absolute', opacity: 0}}
/>
)}
<ThreeCanvas width={width} height={height}>
diff --git a/packages/player-example/src/TimeDisplay.tsx b/packages/player-example/src/TimeDisplay.tsx
index 63f4a37db0..38f28f6fcc 100644
--- a/packages/player-example/src/TimeDisplay.tsx
+++ b/packages/player-example/src/TimeDisplay.tsx
@@ -3,7 +3,7 @@ import React from 'react';
import {useCurrentPlayerFrame} from './use-current-player-frame';
export const TimeDisplay: React.FC<{
- readonly playerRef: React.RefObject<PlayerRef>;
+ readonly playerRef: React.RefObject<PlayerRef | null>;
}> = ({playerRef}) => {
const frame = useCurrentPlayerFrame(playerRef);
diff --git a/packages/player-example/src/use-current-player-frame.ts b/packages/player-example/src/use-current-player-frame.ts
index 744d2e99c3..59542b52f4 100644
--- a/packages/player-example/src/use-current-player-frame.ts
+++ b/packages/player-example/src/use-current-player-frame.ts
@@ -1,7 +1,9 @@
import {CallbackListener, PlayerRef} from '@remotion/player';
import {useCallback, useSyncExternalStore} from 'react';
-export const useCurrentPlayerFrame = (ref: React.RefObject<PlayerRef>) => {
+export const useCurrentPlayerFrame = (
+ ref: React.RefObject<PlayerRef | null>,
+) => {
const subscribe = useCallback(
(onStoreChange: (newVal: number) => void) => {
const {current} = ref;