react 获取 dom 节点的信息

import { useRef, useState, useEffect } from "react";

const test = () => {

    const domHeight = useRef();
    const { current } = domHeight;

    const [divHeight, setDivHeight] = useState(0);

    useEffect(() => {
      if(current) {
        setDivHeight(current.clientHeight)
      }
    }, [current])

    return (
      <div ref={domHeight} style={
   
   {height: 100}}>
        {divHeight}
      </div>
    )
}

export default test;

猜你喜欢

转载自blog.csdn.net/mChales_Liu/article/details/113950416