Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

useResizeObserver fails to unobserve due to ref being null #1190

Closed
karlpetersson opened this issue Oct 23, 2020 · 4 comments
Closed

useResizeObserver fails to unobserve due to ref being null #1190

karlpetersson opened this issue Oct 23, 2020 · 4 comments
Labels
bug Something isn't working good first issue Good for newcomers waiting Waiting on Issue Author

Comments

@karlpetersson
Copy link

karlpetersson commented Oct 23, 2020

馃悰 Bug Report

The cleanup function of the useEffect in useResizeObserver can in some cases call unobserve with an empty ref, which will throw a TypeError (Failed to execute 'unobserve' on 'ResizeObserver': parameter 1 is not of type 'Element'.).

return () => {
resizeObserverInstance.unobserve(ref.current);
};

At least, this is what I think happens. If I add a check for ref.current existing, it works fine.

This seems like it happens when for example rendering a ScrollView inside a modal/popover that unmounts before the cleanup effect is called, in my case I was trying to use a Virtualizer inside a Popover (see code example). For some reason that I can't figure out, this doesn't happen for the react-spectrum Picker component, although it uses a Virtualizer in this same sense.

Here's a codesandbox where this issue is present. Opening then closing the Select throws the mentioned error. https://codesandbox.io/s/affectionate-vaughan-bcy0f?file=/src/App.tsx

馃 Expected Behavior

馃槸 Current Behavior

馃拋 Possible Solution

I think changing the cleanup function in the useEffect in useResizeObserver to something like this could suffice:

  return () => {
    if (ref.current !== null) {
      resizeObserverInstance.unobserve(ref.current)
    }
  };

馃敠 Context

I am building a custom Select component using the react-aria hooks as well as the Virtualizer component.

馃捇 Code Sample

https://codesandbox.io/s/affectionate-vaughan-bcy0f?file=/src/App.tsx

馃實 Your Environment

Software Version(s)
react-spectrum
Browser
Operating System

馃Б Your Company/Team

Visly

馃暦 Tracking Issue (optional)

@LFDanLu LFDanLu added the bug Something isn't working label Oct 23, 2020
@LFDanLu
Copy link
Member

LFDanLu commented Oct 23, 2020

@karlpetersson Thanks for reporting this, I think your solution is reasonable. My guess would be that it works for Picker due to the transition the overlay wrapping the popover has.

@snowystinger
Copy link
Member

I think we actually need to just store the ref.current that we use in the setup so that we can re-use it for the tear down
following the pattern described here
https://reactjs.org/blog/2020/08/10/react-v17-rc.html#effect-cleanup-timing

@snowystinger snowystinger added the good first issue Good for newcomers label Oct 23, 2020
@snowystinger
Copy link
Member

Looks like @LFDanLu has it in his PR already https://github.com/adobe/react-spectrum/pull/1145/files#diff-fa757bcd677038369ad3ed26cca2ff8d645c63dc009266f3d1aeb0e7bdca8a0dR16

@snowystinger
Copy link
Member

The PR has been merged, can you verify that it fixed your issue?

@LFDanLu LFDanLu added the waiting Waiting on Issue Author label Dec 2, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working good first issue Good for newcomers waiting Waiting on Issue Author
Projects
None yet
Development

No branches or pull requests

4 participants