无法获得正确的客户端宽度或客户端高度与反应引用


constructor(props) {
    super(props)
    this.ref = React.createRef()
}
componentDidMount() {
    const { clientWidth, clientHeight } = this.ref.current
    console.log(this.ref.current)
    console.log(clientWidth, clientHeight)
}
render() {
    return (
        <div ref={this.ref} style={{width: '100%', height: '100%'}}></div>
    )
}

我想获取元素的宽度和高度

第一个日志包含正确的值

但是第二个日志说不同

为什么?

第一个日志:

clientHeight:783
clientLeft:0
clientTop:0
clientWidth:1385

第二个日志:

1920 248

现在我已经想通了,chrome控制台日志有时很慢,到记录时,宽度和高度已经更改。

相关内容

最新更新