Skip to content

useResizeObserver - not triggered on first render for older browsers #6091

Open
@tomekancu

Description

@tomekancu

Provide a general summary of the issue here

If useResizeObserver is used on current browsers, onResize is fired on first use.
For older browsers that don't support ResizeObserver, it is fired after the first resize of the browser window.

🤔 Expected Behavior?

onResize is triggered on first render

😯 Current Behavior

onResize is not triggered on first use as in supported browsers

💁 Possible Solution

We should call onResize in useEffect for an unsupported browser condition.

if (!hasResizeObserver()) {
window.addEventListener('resize', onResize, false);
return () => {
window.removeEventListener('resize', onResize, false);
};
} else {

🔦 Context

The original ResizeObserver is called on the observe method.
For the mock implementation with the resize event, we do not have this behaviour. The first onResize must be called manually.

🖥️ Steps to Reproduce

Codesandbox with "simulated" old browser by setting window.ResizeObserver = undefined
https://codesandbox.io/p/sandbox/useresizeobserver-not-triggered-on-init-hx3mkm
or use the hook in older browsers like Firefox 68.0

Version

1.1.1

What browsers are you seeing the problem on?

Firefox

If other, please specify.

No response

What operating system are you using?

Mac

🧢 Your Company/Team

No response

🕷 Tracking Issue

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions