简体   繁体   English

如何在 ReactJS 中获取 div 或元素高度

[英]How to get div or element height in ReactJS

I am trying to get div element in reactJS but I am not able to achieve it.我正在尝试在 reactJS 中获取 div 元素,但我无法实现。 I need some expert to look into my code and resolve my issue or new code snippet would be appreciated.我需要一些专家来查看我的代码并解决我的问题,否则将不胜感激。

Thanks谢谢

Code代码

    this.divElement = React.createRef()

    const height = this.divElement.clientHeight
    this.setState({ height })

     <div
      className="col-2-3"
      ref={(divElement) => {
      this.divElement = divElement
      }}
      >
     </div>

You should use the current property:您应该使用current属性:

this.divElement = React.createRef()

const height = this.divElement.current.clientHeight
this.setState({ height })

 <div
  className="col-2-3"
  ref={this.divElement}
  >
 </div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM