react-use icon indicating copy to clipboard operation
react-use copied to clipboard

useScroll not updating x,y

Open lucasbara opened this issue 3 years ago • 3 comments

What is the current behavior?

Steps to reproduce it and if possible a minimal demo of the problem. Your bug will get fixed much faster if we can run your code and it doesn't have extra dependencies other than react-use. Paste the link to your JSFiddle or CodeSandbox example below:

https://codesandbox.io/s/react-use-issue-ry7zk

What is the expected behavior?

useScroll should update with the current position

A little about versions:

  • OS: Windows 10
  • Browser (vendor and version): Chrome 103
  • React: 17.0.2
  • react-use: 17.3.1
  • _Did this worked in the previous package version? idk

lucasbara avatar Jun 23 '22 14:06 lucasbara

+1

CHU295 avatar Jun 29 '22 10:06 CHU295

i think you can't use it like this:

const ref = useRef(null)
return (
    active && <div ref={ref}></div>
)

first time when react render, active is false and ref.current is null,then you put ref in useScroll,which make hook get null

i think it's better to make active control style: {display: active ? 'block' : 'none''} attribute

SuperZ3 avatar Jul 29 '22 16:07 SuperZ3

React doesn't listen for ref updates, so I think this is an anti-pattern.

childrentime avatar Sep 19 '22 03:09 childrentime

I got it working in this way:

  const [scrollEl, setScrollEl] = useState<HTMLDivElement>(null);
  const { x, y } = useScroll({ current: scrollEl });

  return (
    <>
      {active && <div ref={setScrollEl}></div>}
    </>
  )

Mholy avatar Dec 09 '22 12:12 Mholy