Skip to content

πŸŽ₯ Make videos programmatically with React

`remotion`: Preserve component state during Fast Refresh

Jonny Burger845bd1dce82c
diff --git a/packages/core/src/use-lazy-component.ts b/packages/core/src/use-lazy-component.ts
index fb382867b1..1c732430a6 100644
--- a/packages/core/src/use-lazy-component.ts
+++ b/packages/core/src/use-lazy-component.ts
@@ -3,7 +3,7 @@ import type {
ComponentType,
ExoticComponent,
} from 'react';
-import React, {useMemo} from 'react';
+import React, {useMemo, useRef} from 'react';
import type {CompProps} from './Composition.js';
type LazyExoticComponent<T extends ComponentType<any>> = ExoticComponent<
@@ -22,6 +22,29 @@ export const useLazyComponent = <Props>({
componentName: string;
noSuspense: boolean;
}): LazyExoticComponent<ComponentType<Props>> | ComponentType<Props> => {
+ // Why a ref + stable wrapper instead of returning compProps.component directly?
+ //
+ // When a user edits their component and saves, React Fast Refresh re-executes
+ // the module, giving compProps.component a new function reference.
+ // Previously, this new reference flowed into useMemo (which depended on
+ // compProps.component), producing a new `lazy` value. Composition.tsx then
+ // rendered `<Comp />` where Comp had a different identity, so React unmounted
+ // the old tree and mounted a fresh one β€” losing all component state.
+ //
+ // To fix this, we store the latest component in a ref (updated every render)
+ // and return a stable Wrapper from useMemo (created once). React sees the same
+ // Wrapper component across Fast Refresh updates, preserves the tree and state,
+ // while the ref ensures the Wrapper always delegates to the latest code.
+ //
+ // To reproduce: use packages/example/src/NewVideo.tsx, edit Component
+ // (e.g. change volume={1} to volume={2}) and save. Without this fix,
+ // Component would fully remount instead of fast-refreshing in place.
+ const componentRef = useRef<ComponentType<Props> | null>(null);
+
+ if ('component' in compProps) {
+ componentRef.current = compProps.component as ComponentType<Props>;
+ }
+
const lazy = useMemo(() => {
if ('component' in compProps) {
// In SSR, suspense is not yet supported, we cannot use React.lazy
@@ -37,7 +60,15 @@ export const useLazyComponent = <Props>({
);
}
- return compProps.component as ComponentType<Props>;
+ const Wrapper = (props: Props) => {
+ const Comp = componentRef.current!;
+ return React.createElement(
+ Comp as React.ComponentType<{}>,
+ props as {},
+ );
+ };
+
+ return Wrapper as ComponentType<Props>;
}
if (
@@ -61,9 +92,11 @@ export const useLazyComponent = <Props>({
// Very important to leave the dependencies as they are, or instead
// the player will remount on every frame.
+ // For the 'component' case, we intentionally do NOT depend on
+ // compProps.component β€” the stable wrapper reads from componentRef instead.
// @ts-expect-error
// eslint-disable-next-line react-hooks/exhaustive-deps
- }, [compProps.component, compProps.lazyComponent]);
+ }, [compProps.lazyComponent]);
return lazy;
};
diff --git a/packages/example/src/NewVideo.tsx b/packages/example/src/NewVideo.tsx
index b0b236152f..65adbff235 100644
--- a/packages/example/src/NewVideo.tsx
+++ b/packages/example/src/NewVideo.tsx
@@ -19,7 +19,7 @@ export const calculateMetadataFn: CalculateMetadataFunction<
};
export const Component = () => {
- return <Video src={src} />;
+ return <Video src={src} volume={1.7} />;
};
export const NewVideoComp = () => {