原 React 监听屏幕尺寸变化
首页 > >    作者:lininn   2019年6月21日 13:50 星期五   热度:2972°   百度已收录  
时间:2019-6-21 13:50   热度:2972° 
1.在componentDidMount()中加入监听器
componentDidMount() {
    this.screenChange();
}
 screenChange() {
     window.addEventListener('resize', this.resize);
 }
resize()方法中,定义了屏幕尺寸变化后需要执行的代码



2.在constructor中绑定resize()
constructor(props) {
    this.resize.bind(this);
}

3.在componentWillUnmount()中移除监听器
componentWillUnmount() {       
    window.removeEventListener('resize',this.resize);
}
注:一定要移除监听器,否则多个组件之间会导致this的指向紊乱!!!


--------------------- 

来源:CSDN 
原文:https://blog.csdn.net/qq_41277245/article/details/79984212 

二维码加载中...
本文作者:lininn      文章标题: 原 React 监听屏幕尺寸变化
本文地址:?post=358
版权声明:若无注明,本文皆为“覆手为雨”原创,转载请保留文章出处。
分享本文至:

返回顶部    首页    手机版本    后花园   会员注册   
版权所有:覆手为雨    站长: lininn