From b13d5998fe030ea54afa5348a0849ec7c2c2c0c2 Mon Sep 17 00:00:00 2001 From: Pine Date: Sat, 5 Sep 2026 00:16:32 +0800 Subject: [PATCH] =?UTF-8?q?fix(=E4=B8=AA=E4=BA=BA=E4=B8=BB=E9=A1=B5):=20?= =?UTF-8?q?=E4=BF=AE=E5=A4=8DBlob=20URL=E7=9A=84useEffect=E4=BE=9D?= =?UTF-8?q?=E8=B5=96=E6=95=B0=E7=BB=84TDZ=E9=94=99=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit useEffect依赖数组用了page?.pageType,但const page声明在hook之后, 渲染时评估依赖数组触发Cannot access 'page' before initialization。 改为data?.page?.pageType,effect内部用data?.page取值。 --- console/src/pages/portal/Opc/UserHomePage.tsx | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/console/src/pages/portal/Opc/UserHomePage.tsx b/console/src/pages/portal/Opc/UserHomePage.tsx index 934fc52..868fcb3 100644 --- a/console/src/pages/portal/Opc/UserHomePage.tsx +++ b/console/src/pages/portal/Opc/UserHomePage.tsx @@ -65,17 +65,19 @@ export default function UserHomePage() { // HTML 模式用 Blob URL 替代 srcDoc:srcDoc 的 about:srcdoc 不是有效 URL, // 用户 HTML 内的 hash 路由()会导致 iframe 导航到 about:blank 白屏。 // Blob URL 是真正的 URL,hash 导航 / localStorage / 所有 Web API 均正常。 + // 注意:依赖数组用 data?.page 而非 page,因为 const page = data.page 声明在本 hook 之后。 const [blobUrl, setBlobUrl] = useState(null); useEffect(() => { - if (page?.pageType === "html" && page.htmlContent) { - const blob = new Blob([page.htmlContent], { type: "text/html;charset=utf-8" }); + const p = data?.page; + if (p?.pageType === "html" && p.htmlContent) { + const blob = new Blob([p.htmlContent], { type: "text/html;charset=utf-8" }); const url = URL.createObjectURL(blob); setBlobUrl(url); return () => URL.revokeObjectURL(url); } setBlobUrl(null); return undefined; - }, [page?.pageType, page?.htmlContent]); + }, [data?.page?.pageType, data?.page?.htmlContent]); const load = useCallback(async () => { if (!uid) return;