Skip to content

🎥 Make videos programmatically with React

New API: `useRemotionEnvironment`

Igor Samokhovets87517d85d48d
diff --git a/packages/core/src/get-remotion-environment.ts b/packages/core/src/get-remotion-environment.ts
index 103b7b4065..02fb489a2e 100644
--- a/packages/core/src/get-remotion-environment.ts
+++ b/packages/core/src/get-remotion-environment.ts
@@ -15,7 +15,8 @@ const getEnvString = (): 'env' => {
};
/*
- * @description Provides information about the Remotion Environment
+ * @description Provides information about the Remotion Environment.
+ * @note Prefer using the `useRemotionEnvironment()` hook as it will support future scoped contexts for browser rendering scenarios.
* @see [Documentation](https://remotion.dev/docs/get-remotion-environment)
*/
export const getRemotionEnvironment = (): RemotionEnvironment => {
diff --git a/packages/core/src/index.ts b/packages/core/src/index.ts
index e363363e21..05eeae9f15 100644
--- a/packages/core/src/index.ts
+++ b/packages/core/src/index.ts
@@ -169,6 +169,7 @@ export {
Translation,
useCurrentScale,
} from './use-current-scale';
+export {useRemotionEnvironment} from './use-remotion-environment.js';
export * from './use-video-config.js';
export * from './version.js';
export * from './video-config.js';
diff --git a/packages/core/src/use-remotion-environment.ts b/packages/core/src/use-remotion-environment.ts
new file mode 100644
index 0000000000..2696aa2df7
--- /dev/null
+++ b/packages/core/src/use-remotion-environment.ts
@@ -0,0 +1,12 @@
+import {
+ getRemotionEnvironment,
+ type RemotionEnvironment,
+} from './get-remotion-environment';
+
+/**
+ * @description A React hook that provides information about the current Remotion Environment. This is the preferred way to access environment information in React components as it will support future scoped contexts.
+ * @see [Documentation](https://remotion.dev/docs/use-remotion-environment)
+ */
+export const useRemotionEnvironment = (): RemotionEnvironment => {
+ return getRemotionEnvironment();
+};
diff --git a/packages/docs/docs/get-remotion-environment.mdx b/packages/docs/docs/get-remotion-environment.mdx
index c0f5a59cab..49940cc553 100644
--- a/packages/docs/docs/get-remotion-environment.mdx
+++ b/packages/docs/docs/get-remotion-environment.mdx
@@ -8,6 +8,10 @@ crumb: 'API'
# getRemotionEnvironment()<AvailableFrom v="4.0.25" />
With the `getRemotionEnvironment()` function, you can retrieve information about the current Remotion Environment.
+
+:::info
+Consider using the [`useRemotionEnvironment()`](/docs/use-remotion-environment) hook instead, as it is future-proof for browser rendering scenarios.
+:::
It returns an object with the following properties:
- `isStudio`: Whether the function got called in the [Remotion Studio](/docs/cli/studio).
@@ -47,5 +51,6 @@ A more realistic use case: You might want to debounce a request during editing i
## See also
- [Source code for this function](https://github.com/remotion-dev/remotion/blob/main/packages/core/src/get-remotion-environment.ts)
+- [useRemotionEnvironment()](/docs/use-remotion-environment) - React hook version (recommended for components)
- [useVideoConfig()](/docs/use-video-config)
- [`<OffthreadVideo/> while rendering`](/docs/miscellaneous/snippets/offthread-video-while-rendering)
diff --git a/packages/docs/docs/use-remotion-environment.mdx b/packages/docs/docs/use-remotion-environment.mdx
new file mode 100644
index 0000000000..d721d05738
--- /dev/null
+++ b/packages/docs/docs/use-remotion-environment.mdx
@@ -0,0 +1,57 @@
+---
+image: /generated/articles-docs-use-remotion-environment.png
+title: useRemotionEnvironment()
+id: use-remotion-environment
+crumb: 'API'
+---
+
+# useRemotionEnvironment()<AvailableFrom v="4.0.342" />
+
+A React hook that provides information about the current Remotion Environment. This is the preferred way to access environment information in React components.
+
+It returns an object with the following properties:
+
+- `isStudio`: Whether the hook got called in the [Remotion Studio](/docs/cli/studio).
+- `isRendering`: Whether the hook got called in a render.
+- `isPlayer`: Whether a [`<Player>`](/docs/player) is mounted on the current page.
+- `isReadOnlyStudio`: Whether in a [statically deployed studio](https://www.remotion.dev/docs/studio/deploy-static), where the [`@remotion/studio`](/docs/studio/api) APIs cannot be used
+
+This can be useful if you want components to behave differently depending on the environment.
+
+### Example
+
+```tsx twoslash
+import React from 'react';
+import {useRemotionEnvironment} from 'remotion';
+
+export const MyComp: React.FC = () => {
+ const {isStudio, isPlayer, isRendering} = useRemotionEnvironment();
+
+ if (isStudio) {
+ return <div>I'm in the Studio!</div>;
+ }
+
+ if (isPlayer) {
+ return <div>I'm in the Player!</div>;
+ }
+
+ if (isRendering) {
+ return <div>I'm Rendering</div>;
+ }
+
+ return <div>Hello World!</div>;
+};
+```
+
+## Why use this hook instead of `getRemotionEnvironment()`?
+
+While `getRemotionEnvironment()` works for the current use cases, `useRemotionEnvironment()` is future-proof for browser rendering scenarios where preview and render might happen on the same page simultaneously.
+
+The hook helps to scope the environment information to the context of component that calls it, rather than globally.
+
+## See also
+
+- [Source code for this function](https://github.com/remotion-dev/remotion/blob/main/packages/core/src/use-remotion-environment.ts)
+- [getRemotionEnvironment()](/docs/get-remotion-environment)
+- [useVideoConfig()](/docs/use-video-config)
+- [`<OffthreadVideo/> while rendering`](/docs/miscellaneous/snippets/offthread-video-while-rendering)
diff --git a/packages/docs/sidebars.ts b/packages/docs/sidebars.ts
index 44dccaf8e0..cb5555f121 100644
--- a/packages/docs/sidebars.ts
+++ b/packages/docs/sidebars.ts
@@ -170,6 +170,7 @@ const sidebars: SidebarsConfig = {
'use-buffer-state',
'use-current-frame',
'use-current-scale',
+ 'use-remotion-environment',
'use-video-config',
'version',
'video',