相关疑难解决方法(0)

ReactJS - 获取元素的高度

在React呈现该元素后,如何获得元素的高度?

HTML

<div id="container">
<!-- This element's contents will be replaced with your component. -->
<p>
jnknwqkjnkj<br>
jhiwhiw (this is 36px height)
</p>
</div>
Run Code Online (Sandbox Code Playgroud)

ReactJS

var DivSize = React.createClass({

  render: function() {
    let elHeight = document.getElementById('container').clientHeight
    return <div className="test">Size: <b>{elHeight}px</b> but it should be 18px after the render</div>;
  }
});

ReactDOM.render(
  <DivSize />,
  document.getElementById('container')
);
Run Code Online (Sandbox Code Playgroud)

结果

Size: 36px but it should be 18px after the render
Run Code Online (Sandbox Code Playgroud)

它正在计算渲染前的容器高度(36px).我想在渲染后获得高度.在这种情况下,正确的结果应该是18px.

https://jsfiddle.net/69z2wepo/29800/

javascript reactjs

87
推荐指数
8
解决办法
13万
查看次数

标签 统计

javascript ×1

reactjs ×1