Get div's offsetTop positions in React

前端 未结 6 923
暗喜
暗喜 2021-01-30 08:08

I am trying to implement a List view in React. What I am trying to achieve is that to store the list headers informations and register the components and register the scroll ev

6条回答
  •  耶瑟儿~
    2021-01-30 08:47

    A quicker way if you are using React 16.3 and above is by creating a ref in the constructor, then attaching it to the component you wish to use with as shown below.

    ...
    constructor(props){
       ...
       //create a ref
       this.someRefName = React.createRef();
    

    }

    onScroll(){
    let offsetTop = this.someRefName.current.offsetTop;
    

    }

    render(){
    ...
    
    

    }

提交回复
热议问题