Skip to content

Commit edbff12

Browse files
committed
feat: loading 组件添加静态方法
1 parent c0f9605 commit edbff12

File tree

1 file changed

+22
-2
lines changed

1 file changed

+22
-2
lines changed

src/components/loading/index.tsx

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
1-
import { memo } from 'react';
1+
import { Root, createRoot } from 'react-dom/client';
22

33
import './index.scss';
44

5+
let container: HTMLDivElement | null = null;
6+
let root: Root | null = null;
7+
58
/** Loading组件示例,可替换为ui库的loading组件作二次封装 */
69
function Loading() {
710
return (
@@ -30,4 +33,21 @@ function Loading() {
3033
);
3134
}
3235

33-
export default memo(Loading);
36+
Loading.show = () => {
37+
if (container || root) return;
38+
container = document.createElement('div');
39+
container.setAttribute('id', 'pub-loading');
40+
root = createRoot(container);
41+
root.render(<Loading />);
42+
document.body.appendChild(container);
43+
};
44+
45+
Loading.hide = () => {
46+
if (container && root) {
47+
root.unmount();
48+
document.body.removeChild(container);
49+
container = root = null;
50+
}
51+
};
52+
53+
export default Loading;

0 commit comments

Comments
 (0)