From 13b62d0199aab83dcdc3b5e10004c0837e670fdf Mon Sep 17 00:00:00 2001 From: Alexander Zinchuk Date: Mon, 16 Aug 2021 20:42:16 +0300 Subject: [PATCH] [Perf] Portal: Fix infinitely creating DOM nodes --- src/components/ui/Portal.ts | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/components/ui/Portal.ts b/src/components/ui/Portal.ts index 2c73428e1..7ce2563d8 100644 --- a/src/components/ui/Portal.ts +++ b/src/components/ui/Portal.ts @@ -8,7 +8,10 @@ type OwnProps = { }; const Portal: FC = ({ containerId, className, children }) => { - const elementRef = useRef(document.createElement('div')); + const elementRef = useRef(); + if (!elementRef.current) { + elementRef.current = document.createElement('div'); + } useLayoutEffect(() => { const container = document.querySelector(containerId || '#portals'); @@ -16,7 +19,7 @@ const Portal: FC = ({ containerId, className, children }) => { return undefined; } - const element = elementRef.current; + const element = elementRef.current!; if (className) { element.classList.add(className); }