[Perf] Portal: Fix infinitely creating DOM nodes
This commit is contained in:
parent
362b5a7876
commit
13b62d0199
@ -8,7 +8,10 @@ type OwnProps = {
|
|||||||
};
|
};
|
||||||
|
|
||||||
const Portal: FC<OwnProps> = ({ containerId, className, children }) => {
|
const Portal: FC<OwnProps> = ({ containerId, className, children }) => {
|
||||||
const elementRef = useRef(document.createElement('div'));
|
const elementRef = useRef<HTMLDivElement>();
|
||||||
|
if (!elementRef.current) {
|
||||||
|
elementRef.current = document.createElement('div');
|
||||||
|
}
|
||||||
|
|
||||||
useLayoutEffect(() => {
|
useLayoutEffect(() => {
|
||||||
const container = document.querySelector<HTMLDivElement>(containerId || '#portals');
|
const container = document.querySelector<HTMLDivElement>(containerId || '#portals');
|
||||||
@ -16,7 +19,7 @@ const Portal: FC<OwnProps> = ({ containerId, className, children }) => {
|
|||||||
return undefined;
|
return undefined;
|
||||||
}
|
}
|
||||||
|
|
||||||
const element = elementRef.current;
|
const element = elementRef.current!;
|
||||||
if (className) {
|
if (className) {
|
||||||
element.classList.add(className);
|
element.classList.add(className);
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user