#1532·opentui

`useLayoutEffect` 看到上一个提交的布局,而不是刚刚提交的布局

作者: letrquan创建于 2026年9月24日更新于 2026年9月24日

// repro.tsx -- run with bun repro.tsx. Deterministic, no PTY needed. import { createTestRenderer } from "@opentui/core/testing"; import { createRoot } from "@opentui/React"; import React, { useLayoutEffect, useRef, useState } from "React";

const sleep = (ms: number) => new Promise((r) => setTimeout(r, ms)); const log: string[] = []; let setShowSibling: (v: boolean) => void | null = null;

function App() { const [showSibling, setShow] = useState(false); const wrapperRef = useRef(null); setShowSibling = setShow;

useLayoutEffect(() => { log.push([sync] showSibling=${showSibling} height measured INSIDE the effect = ${wrapperRef.current?.height}); }, [showSibling]);

return ( anchor {showSibling && ( sib 1 sib 2 sib 3 )} ); }

const { renderer, renderOnce, flush } = await createTestRenderer({ width: 20, height: 10 }); const outsideHeight = () => renderer.root.getChildren()[0]?.height;

const root = createRoot(renderer as any); root.render(); await sleep(100); await renderOnce(); await flush(); log.push(--- mounted; height measured from OUTSIDE any effect = ${outsideHeight()} ---);

setShowSibling!(true); await sleep(100); await renderOnce(); await flush(); log.push(--- after setShowSibling(true); height measured from OUTSIDE = ${outsideHeight()} ---);

setShowSibling!(false); await sleep(100); await …

内容来源: anomalyco/opentui