-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBundleComponent.tsx
More file actions
55 lines (45 loc) · 1.42 KB
/
Copy pathBundleComponent.tsx
File metadata and controls
55 lines (45 loc) · 1.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import * as React from 'react';
import { useBundle } from '../hook';
import { DynamicComponent } from './DynamicComponent';
interface ILoaderProps {
children?: any
[key:string]: any
}
interface IFallbackProps {
error: Error
children?: any
[key:string]: any
}
interface IBundleProps {
path: string
name: string
component?: string|string[]
children?: any
onError?: (error:Error) => void
LoaderComponent?: React.ComponentType<ILoaderProps>
FallbackComponent?: React.ComponentType<IFallbackProps>
}
export function BundleComponent({ path, name, component, children, LoaderComponent, FallbackComponent, onError, ...props }: IBundleProps) {
const { isLoading, data, error } = useBundle({ path, name, component });
const renderError = (error) => {
onError && onError(error);
if (FallbackComponent)
return React.createElement(FallbackComponent, { error, ...props }, children);
return <div>{(error && error.message) ?? 'Loadable was unable to render component'}</div>;
};
const renderLoader = () => {
if (LoaderComponent)
return React.createElement(LoaderComponent, { ...props }, children);
return <div>Loading ...</div>;
};
if (isLoading && !data)
return renderLoader();
if (error || (!data && !isLoading))
return renderError(error);
else
return (
<DynamicComponent component={data} {...props} >
{children}
</DynamicComponent>
);
}