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>
)
}
I want to get width and height of an element
The first log contains correct values
But the second log says different
Why?
first log:
clientHeight:783
clientLeft:0
clientTop:0
clientWidth:1385
second log:
1920 248
Now I have figured it out, chrome console log sometime is very slow, by the time it logs, the width and height already changed.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.