Skip to content

🎥 Make videos programmatically with React

`remotion`: Avoid unnecessary state updates at the beginning of the render

Jonny Burger89176c1bd5f0
diff --git a/packages/core/src/RemotionRoot.tsx b/packages/core/src/RemotionRoot.tsx
index 1674bb9893..4e563f3348 100644
--- a/packages/core/src/RemotionRoot.tsx
+++ b/packages/core/src/RemotionRoot.tsx
@@ -52,11 +52,26 @@ export const RemotionRoot: React.FC<{
window.remotion_setFrame = (f: number, composition: string, attempt) => {
window.remotion_attempt = attempt;
const id = delayRender(`Setting the current frame to ${f}`);
- setFrame((s) => ({
- ...s,
- [composition]: f,
- }));
- requestAnimationFrame(() => continueRender(id));
+
+ let avoidUpdate = false;
+
+ setFrame((s) => {
+ const currentFrame = s[composition] ?? window.remotion_initialFrame;
+ // Avoid cloning the object
+ if (currentFrame === f) {
+ avoidUpdate = true;
+ continueRender(id);
+ return s;
+ }
+
+ return {
+ ...s,
+ [composition]: f,
+ };
+ });
+ if (!avoidUpdate) {
+ requestAnimationFrame(() => continueRender(id));
+ }
};
window.remotion_isPlayer = false;
diff --git a/packages/core/src/timeline-position-state.ts b/packages/core/src/timeline-position-state.ts
index cd392b4dfd..8dd6622b5b 100644
--- a/packages/core/src/timeline-position-state.ts
+++ b/packages/core/src/timeline-position-state.ts
@@ -1,5 +1,6 @@
import type {MutableRefObject} from 'react';
import {createContext, useContext, useMemo} from 'react';
+import {getRemotionEnvironment} from './get-remotion-environment.js';
import {useVideo} from './use-video.js';
export type PlayableMediaTag = {
@@ -65,10 +66,15 @@ export const getFrameForComposition = (composition: string) => {
const item = localStorage.getItem(makeKey()) ?? '{}';
const obj: CurrentTimePerComposition = JSON.parse(item);
- return obj[composition]
- ? Number(obj[composition])
- : (typeof window === 'undefined' ? 0 : window.remotion_initialFrame ?? 0) ??
- 0;
+ if (obj[composition] !== undefined) {
+ return Number(obj[composition]);
+ }
+
+ if (typeof window === 'undefined') {
+ return 0;
+ }
+
+ return window.remotion_initialFrame ?? 0;
};
export const useTimelinePosition = (): number => {
@@ -83,7 +89,7 @@ export const useTimelinePosition = (): number => {
const unclamped =
state.frame[videoConfig.id] ??
- (typeof window !== 'undefined' && window.remotion_isPlayer
+ (getRemotionEnvironment().isPlayer
? 0
: getFrameForComposition(videoConfig.id));
diff --git a/packages/core/src/use-current-frame.ts b/packages/core/src/use-current-frame.ts
index 3336120a4c..92056fa40f 100644
--- a/packages/core/src/use-current-frame.ts
+++ b/packages/core/src/use-current-frame.ts
@@ -1,6 +1,7 @@
import {useContext} from 'react';
import {CanUseRemotionHooks} from './CanUseRemotionHooks.js';
import {SequenceContext} from './SequenceContext.js';
+import {getRemotionEnvironment} from './get-remotion-environment.js';
import {useTimelinePosition} from './timeline-position-state.js';
/**
@@ -10,7 +11,7 @@ import {useTimelinePosition} from './timeline-position-state.js';
export const useCurrentFrame = (): number => {
const canUseRemotionHooks = useContext(CanUseRemotionHooks);
if (!canUseRemotionHooks) {
- if (typeof window !== 'undefined' && window.remotion_isPlayer) {
+ if (getRemotionEnvironment().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`,
);