Skip to content

🎥 Make videos programmatically with React

`remotion`: Handle being imported in a React Server Component

Jonny Burger380cad4f653f
diff --git a/packages/core/src/_check-rsc.ts b/packages/core/src/_check-rsc.ts
new file mode 100644
index 0000000000..f7eaaf0252
--- /dev/null
+++ b/packages/core/src/_check-rsc.ts
@@ -0,0 +1,17 @@
+import {createContext} from 'react';
+
+if (typeof createContext !== 'function') {
+ const err = [
+ 'Remotion requires React.createContext, but it is "undefined".',
+ 'If you are in a React Server Component, turn it into a client component by adding "use client" at the top of the file.',
+ '',
+ 'Before:',
+ ' import {useCurrentFrame} from "remotion";',
+ '',
+ 'After:',
+ ' "use client";',
+ ' import {useCurrentFrame} from "remotion";',
+ ];
+
+ throw new Error(err.join('\n'));
+}
diff --git a/packages/core/src/index.ts b/packages/core/src/index.ts
index 88ed8ff801..a5602569fe 100644
--- a/packages/core/src/index.ts
+++ b/packages/core/src/index.ts
@@ -1,3 +1,4 @@
+import './_check-rsc.js';
import './asset-types.js';
import {Clipper} from './Clipper.js';
import type {Codec} from './codec.js';
diff --git a/packages/player/src/_check-rsc.ts b/packages/player/src/_check-rsc.ts
new file mode 100644
index 0000000000..f7eaaf0252
--- /dev/null
+++ b/packages/player/src/_check-rsc.ts
@@ -0,0 +1,17 @@
+import {createContext} from 'react';
+
+if (typeof createContext !== 'function') {
+ const err = [
+ 'Remotion requires React.createContext, but it is "undefined".',
+ 'If you are in a React Server Component, turn it into a client component by adding "use client" at the top of the file.',
+ '',
+ 'Before:',
+ ' import {useCurrentFrame} from "remotion";',
+ '',
+ 'After:',
+ ' "use client";',
+ ' import {useCurrentFrame} from "remotion";',
+ ];
+
+ throw new Error(err.join('\n'));
+}
diff --git a/packages/player/src/index.ts b/packages/player/src/index.ts
index 8e636507fc..9bc8f9bfd6 100644
--- a/packages/player/src/index.ts
+++ b/packages/player/src/index.ts
@@ -1,3 +1,4 @@
+import './_check-rsc.js';
import {BufferingIndicator} from './BufferingIndicator.js';
import {calculateCanvasTransformation} from './calculate-scale.js';
import {PlayerEventEmitterContext} from './emitter-context.js';