From 43c7a4451b6d0c5cbae49b946c0b53d07415ef03 Mon Sep 17 00:00:00 2001 From: Archmonger <16909269+Archmonger@users.noreply.github.com> Date: Tue, 21 Jan 2025 03:30:41 -0800 Subject: [PATCH] Fix tests --- .../@reactpy/client/src/components.tsx | 20 ++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/src/js/packages/@reactpy/client/src/components.tsx b/src/js/packages/@reactpy/client/src/components.tsx index 4d29a9ff2..efaa7a759 100644 --- a/src/js/packages/@reactpy/client/src/components.tsx +++ b/src/js/packages/@reactpy/client/src/components.tsx @@ -120,13 +120,8 @@ function ScriptElement({ model }: { model: ReactPyVdom }) { const ref = useRef(null); React.useEffect(() => { - // Fetch the script's content - const scriptContent = model?.children?.filter( - (value): value is string => typeof value == "string", - )[0]; - - // Don't run if the parent element or script content is missing - if (!ref.current || !scriptContent) { + // Don't run if the parent element is missing + if (!ref.current) { return; } @@ -136,8 +131,15 @@ function ScriptElement({ model }: { model: ReactPyVdom }) { scriptElement.setAttribute(k, v); } - // Append the script content to the script element - scriptElement.appendChild(document.createTextNode(scriptContent)); + // Add the script content as text + const scriptContent = model?.children?.filter( + (value): value is string => typeof value == "string", + )[0]; + if (scriptContent) { + scriptElement.appendChild(document.createTextNode(scriptContent)); + } + + // Append the script element to the parent element ref.current.appendChild(scriptElement); // Remove the script element when the component is unmounted