react : 在组件内部获取组件本身

版权声明:转载请注明出处 https://blog.csdn.net/weixin_43586120/article/details/88885097

话不多说,上代码:this.refs.**就可以获取到当前组件的高度

import { Component } from "react";

class Category extends Component{
    toggle=()=>{  
        if(this.refs.category.clientHeight>74){//获取当前组件的高度
            /* 具体操作 */
        }
    }
    render(){
        return (
            <div ref="category">
                <a onClick={()=>{this.toggle()}}>
                    展开<Icon type="up" />
                </a>
            </div>
       );
   }
}

export default Category;

注:UI组件中不能使用ref。

猜你喜欢

转载自blog.csdn.net/weixin_43586120/article/details/88885097