Get div's offsetTop positions in React

前端 未结 6 852
暗喜
暗喜 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 09:09

    Eugene's answer uses the correct function to get the data, but for posterity I'd like to spell out exactly how to use it in React v0.14+ (according to this answer):

      import ReactDOM from 'react-dom';
      //...
      componentDidMount() {
        var rect = ReactDOM.findDOMNode(this)
          .getBoundingClientRect()
      }
    

    Is working for me perfectly, and I'm using the data to scroll to the top of the new component that just mounted.

提交回复
热议问题