Skip to content

πŸŽ₯ Make videos programmatically with React

rename to useBufferState()

Jonny Burger3f32cc144703
diff --git a/packages/core/src/index.ts b/packages/core/src/index.ts
index f216181bbc..6a521474a0 100644
--- a/packages/core/src/index.ts
+++ b/packages/core/src/index.ts
@@ -132,7 +132,7 @@ export * from './spring/index.js';
export {staticFile} from './static-file.js';
export * from './Still.js';
export type {PlayableMediaTag} from './timeline-position-state.js';
-export {useBuffer} from './use-buffer';
+export {useBufferState} from './use-buffer';
export {useCurrentFrame} from './use-current-frame.js';
export * from './use-video-config.js';
export * from './version.js';
diff --git a/packages/core/src/use-buffer.ts b/packages/core/src/use-buffer.ts
index 93f04b069e..c4df877884 100644
--- a/packages/core/src/use-buffer.ts
+++ b/packages/core/src/use-buffer.ts
@@ -1,12 +1,26 @@
import {useContext, useMemo} from 'react';
import {BufferingContextReact} from './buffering';
+import {CanUseRemotionHooks} from './CanUseRemotionHooks';
-export const useBuffer = () => {
- const buffer = useContext(BufferingContextReact);
+export const useBufferState = () => {
+ const canUseRemotionHooks = useContext(CanUseRemotionHooks);
+
+ if (!canUseRemotionHooks) {
+ if (typeof window !== 'undefined' && window.remotion_isPlayer) {
+ throw new Error(
+ `useCurrentFrame can only be called inside a component that was passed to <Player>. See: https://www.remotion.dev/docs/player/examples`,
+ );
+ }
+
+ throw new Error(
+ `useCurrentFrame() can only be called inside a component that was registered as a composition. See https://www.remotion.dev/docs/the-fundamentals#defining-compositions`,
+ );
+ }
+ const buffer = useContext(BufferingContextReact);
if (!buffer) {
- throw new TypeError(
- 'Can only use useBuffer() inside a Remotion composition',
+ throw new Error(
+ 'BufferingContextReact was unexpectedly not found. Most likely your Remotion versions are mismatching.',
);
}
diff --git a/packages/core/src/use-media-buffering.ts b/packages/core/src/use-media-buffering.ts
index 0881216059..efebdd1eb2 100644
--- a/packages/core/src/use-media-buffering.ts
+++ b/packages/core/src/use-media-buffering.ts
@@ -1,12 +1,12 @@
import type React from 'react';
import {useEffect} from 'react';
-import {useBuffer} from './use-buffer';
+import {useBufferState} from './use-buffer';
export const useMediaBuffering = (
element: React.RefObject<HTMLVideoElement | HTMLAudioElement>,
shouldBuffer: boolean,
) => {
- const buffer = useBuffer();
+ const buffer = useBufferState();
useEffect(() => {
let cleanup = () => undefined;
diff --git a/packages/docs/docs/player/buffer-state.mdx b/packages/docs/docs/player/buffer-state.mdx
index f659611b89..027186931f 100644
--- a/packages/docs/docs/player/buffer-state.mdx
+++ b/packages/docs/docs/player/buffer-state.mdx
@@ -15,14 +15,14 @@ Remotion has a native buffer state, which can be used to pause the video when th
### Activating the buffer state
-A component can tell the player to switch into a buffer state by first using the `useBuffer()` hook and then calling `buffer.delayPlayback()`:
+A component can tell the player to switch into a buffer state by first using the `useBufferState()` hook and then calling `buffer.delayPlayback()`:
```tsx twoslash title="MyComp.tsx"
import React from "react";
-import { useBuffer } from "remotion";
+import { useBufferState } from "remotion";
const MyComp: React.FC = () => {
- const buffer = useBuffer();
+ const buffer = useBufferState();
React.useEffect(() => {
const delayHandle = buffer.delayPlayback();
@@ -51,10 +51,10 @@ Use the cleanup function of <code>useEffect()</code> to clear the handle when yo
```tsx twoslash title="❌ Causes problems with React strict mode"
import React, { useState } from "react";
-import { useBuffer } from "remotion";
+import { useBufferState } from "remotion";
const MyComp: React.FC = () => {
- const buffer = useBuffer();
+ const buffer = useBufferState();
const [delayHandle] = useState(() => buffer.delayPlayback()); // πŸ’₯
React.useEffect(() => {
@@ -75,10 +75,10 @@ While the following implementation works in production, it fails in React Strict
```tsx twoslash title="❌ Doesn't clear the buffer handle when seeking to a different portion of a video"
import React, { useState } from "react";
-import { useBuffer } from "remotion";
+import { useBufferState } from "remotion";
const MyComp: React.FC = () => {
- const buffer = useBuffer();
+ const buffer = useBufferState();
const [delayHandle] = useState(() => buffer.delayPlayback()); // πŸ’₯
React.useEffect(() => {
diff --git a/packages/example/src/BufferState/Simple.tsx b/packages/example/src/BufferState/Simple.tsx
index 48cd5332d0..b69dad11a3 100644
--- a/packages/example/src/BufferState/Simple.tsx
+++ b/packages/example/src/BufferState/Simple.tsx
@@ -1,8 +1,8 @@
import React, {useEffect, useState} from 'react';
-import {AbsoluteFill, Series, useBuffer} from 'remotion';
+import {AbsoluteFill, Series, useBufferState} from 'remotion';
const Inside: React.FC = () => {
- const buffer = useBuffer();
+ const buffer = useBufferState();
const [block] = useState(() => buffer.delayPlayback());
useEffect(() => {
diff --git a/packages/player-example/src/BufferState.tsx b/packages/player-example/src/BufferState.tsx
index 4f919a2abd..0873f69a43 100644
--- a/packages/player-example/src/BufferState.tsx
+++ b/packages/player-example/src/BufferState.tsx
@@ -1,8 +1,8 @@
import React, {useEffect} from 'react';
-import {AbsoluteFill, Series, useBuffer} from 'remotion';
+import {AbsoluteFill, Series, useBufferState} from 'remotion';
const Inside: React.FC = () => {
- const buffer = useBuffer();
+ const buffer = useBufferState();
useEffect(() => {
const block = buffer.delayPlayback();