From 9dfdc8683d114d09d69b84ae5b9a8e823ce0bd0e Mon Sep 17 00:00:00 2001 From: clean99 Date: Tue, 21 Nov 2023 09:23:07 +0800 Subject: [PATCH] fix: prevent flashing when updating code --- frontend/src/components/Preview.tsx | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/frontend/src/components/Preview.tsx b/frontend/src/components/Preview.tsx index de0134f..3a603ff 100644 --- a/frontend/src/components/Preview.tsx +++ b/frontend/src/components/Preview.tsx @@ -1,3 +1,4 @@ +import { useEffect, useRef } from 'react'; import classNames from "classnames"; import useThrottle from "../hooks/useThrottle"; @@ -8,12 +9,22 @@ interface Props { function Preview({ code, device }: Props) { const throttledCode = useThrottle(code, 200); + const iframeRef = useRef(null); + + useEffect(() => { + const iframe = iframeRef.current; + if (iframe && iframe.contentDocument) { + iframe.contentDocument.open(); + iframe.contentDocument.write(throttledCode); + iframe.contentDocument.close(); + } + }, [throttledCode]); return (