2

我想在渲染后获取 Rect 中元素的行高,为此我正在使用 useRef

const titleLineHeight = titletext.current?.style.lineHeight;

我没有得到任何价值CSSStyleDeclaration

在此处输入图像描述

我不确定如何获得这些值。请帮我

提前致谢 :)

4

1 回答 1

1

使用 getComputedStyle()

https://developer.mozilla.org/en-US/docs/Web/API/Window/getComputedStyle


const paraRef = useRef(null);

  useEffect(() => {
    const paraStyle = paraRef.current && getComputedStyle(paraRef.current);
    console.log(paraStyle.lineHeight);
  }, []);

  return (
    <div className="App">
      <p ref={paraRef}>New text</p>
    </div>
  );

于 2021-11-02T07:58:48.777 回答