[Perf] Portal: Fix infinitely creating DOM nodes

This commit is contained in:
Alexander Zinchuk 2021-08-16 20:42:16 +03:00
parent 362b5a7876
commit 13b62d0199

View File

@ -8,7 +8,10 @@ type OwnProps = {
};
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(() => {
const container = document.querySelector<HTMLDivElement>(containerId || '#portals');
@ -16,7 +19,7 @@ const Portal: FC<OwnProps> = ({ containerId, className, children }) => {
return undefined;
}
const element = elementRef.current;
const element = elementRef.current!;
if (className) {
element.classList.add(className);
}