antonioru / beautiful-react-hooks

🔥 A collection of beautiful and (hopefully) useful React hooks to speed-up your components and hooks development 🔥

Home Page:https://antonioru.github.io/beautiful-react-hooks/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

useResizeObserver doesn't work with changing refs

sandrosc opened this issue · comments

Changing refs, like in the following example, leads to both DOM nodes being observed, since the first node is never unobserved.

const DOMRect = useResizeObserver(first ? ref1 : ref2);

Example: https://codesandbox.io/s/black-dew-36k642?file=/src/App.js

  1. First box is being observed, second box not.
  2. Click button.
  3. Expected: Second box should be observed, first box not. Actual behaviour: Both boxes are observed.
commented

@sandrosc you should use two different useResizeObserver for ref1 and ref2