Skip to content

Instantly share code, notes, and snippets.

@danielpza
Created December 21, 2022 23:50
Show Gist options
  • Save danielpza/96142721f8c30145561270ef3627bf8a to your computer and use it in GitHub Desktop.
Save danielpza/96142721f8c30145561270ef3627bf8a to your computer and use it in GitHub Desktop.
import { ComponentType, Suspense } from 'react';
export function withSuspense<T extends {}>(
Component: ComponentType<T>,
Fallback: ComponentType<Partial<T>>
) {
const displayName = Component.displayName || Component.name || 'Component';
function ComponentWithSuspense(props: T) {
return (
<Suspense fallback={<Fallback />}>
<Component {...(props as T)} />
</Suspense>
);
}
ComponentWithSuspense.displayName = `withSuspense(${displayName})`;
return ComponentWithSuspense;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment