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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Package Responsive: useParentSize is not updating the width and height #1816

Closed
Ti-tanium opened this issue Apr 9, 2024 · 2 comments
Closed

Comments

@Ti-tanium
Copy link

Ti-tanium commented Apr 9, 2024

Hi, I tried using the useParentSize hook. The width and height are always 0 when I resize the parent container.

After looking at the source code, I believe we should add parentRef.current to the dependency array for this useEffect hook:

useEffect(() => {
const LocalResizeObserver =
resizeObserverPolyfill || (window as unknown as PrivateWindow).ResizeObserver;
const observer = new LocalResizeObserver((entries) => {
entries.forEach((entry) => {
const { left, top, width, height } = entry?.contentRect ?? {};
animationFrameID.current = window.requestAnimationFrame(() => {
resize({ width, height, top, left });
});
});
});
if (parentRef.current) observer.observe(parentRef.current);
return () => {
window.cancelAnimationFrame(animationFrameID.current);
observer.disconnect();
resize.cancel();
};
}, [resize, resizeObserverPolyfill]);

Because the parentRef is initially null, so observer.observe(parentRef.current); is never executed, even if parentRef is set to the element after initial render.

@hshoff
Copy link
Member

hshoff commented May 9, 2024

Hi @Ti-tanium could you provide a codesandbox reproducing the issue you're seeing? The useParentSize hook should be working but a lot depends on how it is used.

@hshoff
Copy link
Member

hshoff commented May 27, 2024

Hi @Ti-tanium, closing this as we haven't heard back. Happy to reopen if needed.

@hshoff hshoff closed this as completed May 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants