React componentDidMount 中获取高度是准确的吗?
答案肯定是有准确的时候也有不准确的时候啊
使用ref钩子在componentDidMount 中获取div高度的方法
class App extends React.Component {
state = {
height: 0,
}
componentDidMount() {
this.setState({
height: this.refs.thisDiv.getDOMNode().offsetHeight
}
}
render() {
return <div ref="thisDiv">
<h1>height is {
this.state.height}</h1>
</div>;
}
}
class App extends React.Component {
state = {
height: 0,
}