react ref获取元素高度
import ReactDOM from "react-dom";
class App extends Component {
constructor(props) {
super(props);
this.state = {
width: 0
};
}
componentDidMount() {
const width = this.divElement.clientWidth;
this.setState({ width });
}
render() {
return (
<div className="test" ref={divElement => (this.divElement = divElement)}>
Size: <b>{this.state.width}px</b> but it should be 18px after the render
</div>
);
}
}
const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);