question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Question regarding environments missing ResizeObserver

See original GitHub issue

I have a library where I don’t necessarily want to polyfill ResizeObserver if a device is missing support, meaning I’d like to avoid calling useResizeObserver when ResizeObserver does not exist in DOM.

Due to React semantics, I can’t make the call to the hook conditional.

Would it be reasonable to extend the hook with an option to disable resize detection when it isn’t supported (e.g. by blocking calls to new ResizeObserver internally)?

I know that I can add the hook to a conditionally rendered element in order to prevent it from running if ResizeObserver isn’t in DOM, e.g.:

  { ("ResizeObserver" in window) && <SomeComponentWithResizeObserver callback={setSomeStateOnResize} /> }

However, if my library only exports a hook and no component, that wouldn’t be possible.

I can create a PR if this sounds reasonable.

Issue Analytics

  • State:open
  • Created 10 months ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
martinstarkcommented, Nov 11, 2022

I just used delete in order to quickly test what happens in an environment where ResizeObserver is missing and calling it would throw an error. Setting ref to null prevents calls to ResizeObserver. That delete is not part of my actual code 😬

Real code example would be something like:

  useResizeObserver({
    // prevent crashing in environments that do not support ResizeObserver
    ref: isRoAvailable ? someRef : null,
    onResize: () => {
      // ...
    },
  })
1reaction
ZeeCodercommented, Nov 11, 2022

Great, I’ll leave this issue open until it’s documented, I think it’s an interesting use-case. @martinstark can you just also confirm when you did the implementation if there were anything else that needed to be considered, or if it was just copy-c of the above code? Just so I’d know what to document.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Issues · ZeeCoder/use-resize-observer - GitHub
A React hook that allows you to use a ResizeObserver to measure an ... Question regarding environments missing ResizeObserver to-document.
Read more >
How to mock ResizeObserver to work in unit tests using react ...
My problem is that when I go to run my units test it breaks all my tests and looking at the snapshot it...
Read more >
What is the resize observer API in web API? - Educative.io
The Resize Observer API provides a way to notify an observer if an element it is registered on is re-sized in any way....
Read more >
I just upgraded our lab environment to version 11.3.6. After ...
I received the error message inside Tableau Public when I connected to the MicroStrategy For Tableau Connector web URL. I receive no error ......
Read more >
Responsive Components: a Solution to the Container Queries ...
If you're a web developer, you've probably heard about container queries ... will apply no matter what environment the component is within.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found