Comments (6)
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.
from use-resize-observer.
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: () => {
// ...
},
})
from use-resize-observer.
I think you might be able to achieve what you want with the current hook.
When you don't pass in an element to to hook to be observed, then the hook doesn't create an RO instance, and therefore won't need a polyfill either up until that point.
This lazy-initialization was introduced for SSR compatibility initially, but could be used for this as follows:
import useResizeObserverRaw = 'use-resize-observer';
const isRoAvailable = typeof window !== 'undefined' && ("ResizeObserver" in window);
export const useResizeObserver = () => {
const { ref: refRaw, with, height } = useResizeObserverRaw();
const ref = useCallback((element) => {
if (isRoAvailable) { refRaw(element); }
}, [refRaw, isRoAvailable]);
return useMemo(() => ({ref, width, height}), [ref, width, height])
}
Then you'd use this hook as usual:
const { ref, width = 42, height = 24 } = useResizeObserver();
☝️ Where the default values would be used when no RO is available.
I'm a bit unsure how this would work with SSR, but I think when hydration happens it would call the ref passed in with the new isRoAvailable
variable available to it.
Of course you can do other things as well depending on what you need in the if (isRoAvailable)
check, not just skipping hook initialisation.
Hope this helps.
from use-resize-observer.
Thanks @ZeeCoder, that's great to know. I should have looked more closely at the source. I don't expect this to be a very common usecase, but could be useful to have this behaviour documented 👍
from use-resize-observer.
I'm using the inverted version where I'm passing a ref into the hook, seems to work in an even easier manner:
delete window.ResizeObserver;
// ...
useResizeObserver({
ref: null,
onResize: () => {
// ...
},
});
Triggers no error.
from use-resize-observer.
Thanks for sharing!
That's not something I can recommend in the docs with the delete and all though, so I'll probably just use my original recommendation.
It seems like the idea behind it is already enough to nudge people in the right direction anyway. 👍
from use-resize-observer.
Related Issues (20)
- Externalise useResolvedElement HOT 11
- Are the terms `blockSize` and `inlineSize` in the docs backwards? HOT 1
- Support for <img/> elements using `object-fit` HOT 5
- Future enhancement to return element coordinates
- Support React 19 HOT 1
- SSR HTMLDivElement is not defined in 7.0.0 HOT 2
- Refs not being released on component dismounts leading to memory leaks HOT 7
- useResizeDetector resize method is not triggering in v7.0.1 HOT 7
- Using the same ResizeObserver instance for all hooks for better performance HOT 1
- Safari 15 has ResizeObserver API but it still does not work HOT 14
- ResizeObserverSize is not defined in typescript build
- onResize is not called on mount HOT 3
- Why is it suggested to instal as a dev dependency HOT 2
- No values reported when using React StrictMode HOT 5
- support for SVG elements HOT 9
- Basic usage is not working HOT 3
- Is there any reason we should save this as a dev dependency instead of a runtime dep? HOT 1
- Support getBoundingClientRect() HOT 9
- export TS types HOT 9
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from use-resize-observer.