React 如何使用 ref 获取 div 的高度和宽度?

当前如下:

import React from 'react';

class Index extends React.Component {
    constructor(props) {
        this.ref= React.createRef();
    }
    
    componentDidMount = () => {
        // 都无效,undefined
        console.log(this.ref.getBoundingClientRect);
        console.log(this.ref.current.clientHeight);
    }
    
    render() {
        return (
            ...
            <div ref={this.ref}></div>
            ...
        )
    }
}
阅读 20.4k
3 个回答

getBoundingClientRect 不用加()吗?

新手上路,请多包涵

super()?

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题