注册resize事件监听浏览器屏幕大小

327 阅读1分钟
class Test extends Component {
  constructor(props) {
    super(props);
    this.state = {};
  }
  componentDidMount() {
    window.addEventListener("resize", this.handleResize);
  }
  handleResize = () => {
    //   ref
    console.log(this.mainBox.clientWidth);
    // jQuery
    console.log($(".mainBox").width());
  };
  render() {
    return (
      <div className="mainBox" ref={(ref) => (this.mainBox = ref)}>
        test
      </div>
    );
  }
}

module.exports = Test;