【发布时间】:2021-06-16 16:36:37
【问题描述】:
我正在尝试在 reactJS 中获取 div 元素,但我无法实现它。我需要一些专家来查看我的代码并解决我的问题或新代码 sn-p 将不胜感激。
谢谢
代码
this.divElement = React.createRef()
const height = this.divElement.clientHeight
this.setState({ height })
<div
className="col-2-3"
ref={(divElement) => {
this.divElement = divElement
}}
>
</div>
【问题讨论】:
-
尝试在
componentDidMount方法中设置状态。 -
这能回答你的问题吗? ReactJS - Get Height of an element
-
@MaheerAli 我在 componentDidmount 中做。
-
@WaisKamal no ,实际上我遵循评论中提到的相同但无法找到解决方案
标签: javascript arrays reactjs ecmascript-6