Skip to content

Drop in a screenshot and convert it to clean code (HTML/Tailwind/React/Vue)

Python

Fix preview iframe event listener leak

Abi Rajaa08b630d1c91
diff --git a/frontend/src/components/preview/PreviewComponent.tsx b/frontend/src/components/preview/PreviewComponent.tsx
index 0049500d..80591254 100644
--- a/frontend/src/components/preview/PreviewComponent.tsx
+++ b/frontend/src/components/preview/PreviewComponent.tsx
@@ -1,4 +1,4 @@
-import { useEffect, useRef, useState } from "react";
+import { useCallback, useEffect, useRef, useState } from "react";
import classNames from "classnames";
import useThrottle from "../../hooks/useThrottle";
import EditPopup from "../select-and-edit/EditPopup";
@@ -19,6 +19,9 @@ function PreviewComponent({ code, device, doUpdate }: Props) {
// Select and edit functionality
const [clickEvent, setClickEvent] = useState<MouseEvent | null>(null);
const [scale, setScale] = useState(1);
+ const handleIframeClick = useCallback((event: MouseEvent) => {
+ setClickEvent(event);
+ }, []);
// Add scaling logic
useEffect(() => {
@@ -48,16 +51,30 @@ function PreviewComponent({ code, device, doUpdate }: Props) {
useEffect(() => {
const iframe = iframeRef.current;
- if (iframe) {
- iframe.srcdoc = throttledCode;
+ if (!iframe) return;
+
+ const handleLoad = () => {
+ const body = iframe.contentWindow?.document.body;
+ if (!body) return;
+ body.addEventListener("click", handleIframeClick);
+ };
- // Set up click handler for select and edit funtionality
- iframe.addEventListener("load", function () {
- iframe.contentWindow?.document.body.addEventListener(
- "click",
- setClickEvent
- );
- });
+ iframe.addEventListener("load", handleLoad);
+
+ return () => {
+ iframe.removeEventListener("load", handleLoad);
+ const body = iframe.contentWindow?.document.body;
+ if (body) {
+ body.removeEventListener("click", handleIframeClick);
+ }
+ };
+ }, [handleIframeClick]);
+
+ useEffect(() => {
+ const iframe = iframeRef.current;
+ if (!iframe) return;
+ if (iframe.srcdoc !== throttledCode) {
+ iframe.srcdoc = throttledCode;
}
}, [throttledCode]);