Get div's offsetTop positions in React

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

    I do realize that the author asks question in relation to a class-based component, however I think it's worth mentioning that as of React 16.8.0 (February 6, 2019) you can take advantage of hooks in function-based components.

    Example code:

    import { useRef } from 'react'
    
    function Component() {
      const inputRef = useRef()
    
      return (
        
        
    { const { offsetTop } = inputRef.current ... }} > ) }

提交回复
热议问题