-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathMicro.tsx
More file actions
38 lines (27 loc) · 1.01 KB
/
Micro.tsx
File metadata and controls
38 lines (27 loc) · 1.01 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
import React, { useCallback, useEffect } from 'react';
import useManifest from '../../hooks/useManifest/useManifest';
import { RENDER_FUNCTION_ID, UNMOUNT_FUNCTION_ID } from '../../modules/host/constants';
interface MicroProps {
name: string;
host: string;
manifestSRC: string;
dependencies?: any;
fetchOpts?: any;
}
const Micro = ({ name, host, dependencies, manifestSRC, fetchOpts }: MicroProps) => {
const containerId = `${name}-container`;
const renderHost = useCallback(() => {
const renderFnName = RENDER_FUNCTION_ID + name;
if (!(window as any)[renderFnName]) return;
(window as any)[renderFnName](containerId, dependencies);
}, []);
const dettachHost = () => {
const unmountFnName = UNMOUNT_FUNCTION_ID + name;
if (!(window as any)[unmountFnName]) return;
(window as any)[unmountFnName]();
};
useManifest(renderHost, { name, host, manifestSRC }, fetchOpts);
useEffect(() => dettachHost, []);
return <main id={containerId} />;
};
export default React.memo(Micro);