React下获取width和height
利用ref
保存DOM节点,然后读取clientWidth
,clientHeight
获取宽高参数。
import React, { Component } from 'react';
import 'assets/css/App.css'
export class App extends Component {
constructor(props) {
super(props);
this.saveRef = ref => {this.refDom = ref};
this.handleClick = this.handleClick.bind(this);
}
handleClick(e) {
const {clientWidth, clientHeight} = this.refDom;
console.log('====================================');
console.log(clientWidth, clientHeight, this.refDom);
console.log('====================================');
}
render() {
return (
<div ref={this.saveRef} onClick={this.handleClick} > Loading...</div>
);
}
}
export default App;