Skip to content

Commit

Permalink
Merge pull request #4855 from remotion-dev/error-when-passing-undefin…
Browse files Browse the repository at this point in the history
…ed-to-composition
  • Loading branch information
JonnyBurger authored Feb 4, 2025
2 parents 1a88854 + d863cb6 commit 0c67836
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 10 deletions.
5 changes: 4 additions & 1 deletion packages/core/src/Composition.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,10 @@ export const Composition = <
useContext(CompositionManager);
const video = useVideo();

const lazy = useLazyComponent<Props>(compProps as CompProps<Props>);
const lazy = useLazyComponent<Props>({
compProps: compProps as CompProps<Props>,
componentName: 'Composition',
});
const nonce = useNonce();
const isPlayer = useIsPlayer();
const environment = getRemotionEnvironment();
Expand Down
22 changes: 19 additions & 3 deletions packages/core/src/use-lazy-component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,24 @@ type LazyExoticComponent<T extends ComponentType<any>> = ExoticComponent<
};

// Expected, it can be any component props
export const useLazyComponent = <Props>(
compProps: CompProps<Props>,
): LazyExoticComponent<ComponentType<Props>> => {
export const useLazyComponent = <Props>({
compProps,
componentName,
}: {
compProps: CompProps<Props>;
componentName: string;
}): LazyExoticComponent<ComponentType<Props>> => {
const lazy = useMemo(() => {
if (
'lazyComponent' in compProps &&
typeof compProps.lazyComponent !== 'undefined'
) {
if (typeof compProps.lazyComponent === 'undefined') {
throw new Error(
`A value of \`undefined\` was passed to the \`lazyComponent\` prop. Check the value you are passing to the <${componentName}/> component.`,
);
}

return React.lazy(
compProps.lazyComponent as () => Promise<{
default: ComponentType<Props>;
Expand All @@ -36,6 +46,12 @@ export const useLazyComponent = <Props>(
>;
}

if (typeof compProps.component === 'undefined') {
throw new Error(
`A value of \`undefined\` was passed to the \`component\` prop. Check the value you are passing to the <${componentName}/> component.`,
);
}

return React.lazy(() =>
Promise.resolve({default: compProps.component as ComponentType<Props>}),
);
Expand Down
7 changes: 4 additions & 3 deletions packages/player/src/Player.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -199,9 +199,10 @@ const PlayerFn = <
),
);

const component = Internals.useLazyComponent(
componentProps,
) as LazyExoticComponent<ComponentType<unknown>>;
const component = Internals.useLazyComponent({
compProps: componentProps,
componentName: 'Player',
}) as LazyExoticComponent<ComponentType<unknown>>;

validateInitialFrame({initialFrame, durationInFrames});

Expand Down
7 changes: 4 additions & 3 deletions packages/player/src/Thumbnail.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,10 @@ const ThumbnailFn = <

useImperativeHandle(ref, () => rootRef.current as ThumbnailMethods, []);

const Component = Internals.useLazyComponent(
componentProps,
) as LazyExoticComponent<ComponentType<unknown>>;
const Component = Internals.useLazyComponent({
compProps: componentProps,
componentName: 'Thumbnail',
}) as LazyExoticComponent<ComponentType<unknown>>;

const [emitter] = useState(() => new ThumbnailEmitter());

Expand Down

0 comments on commit 0c67836

Please sign in to comment.