Weird collapsing width behaviour of measureBeforeMount on WidthProvider #2137
Unanswered
mocantimoteidavid
asked this question in
Q&A
Replies: 1 comment
-
|
I could easily achieve my desired result by creating a custom resize observer. I used the Here's my hook: import { useState } from 'react';
import { useResizeDetector } from 'react-resize-detector';
/**
* Hook that measures the width of an HTML element and updates when it changes
*/
const useElementWidthDetector = (
ref: React.MutableRefObject<HTMLElement | null>,
): number | null => {
const [width, setWidth] = useState<number | null>(null);
useResizeDetector({
targetRef: ref,
handleWidth: true,
handleHeight: false,
onResize: () => {
if (ref.current) {
setWidth(ref.current.offsetWidth);
}
},
});
return width;
};
export default useElementWidthDetector;And in my example I use it as such: import ReactGridLayout from "react-grid-layout";
import useElementWidthDetector from "./helpers/useElementWidthDetector"
export default function App() {
const parentRef = React.useRef<HTMLDivElement>(null);
const calculatedWidth = useElementWidthDetector(parentRef);
const widgetsState = [
{ name: "Widget 1", x: 0, y: 0, w: 1, h: 4 },
{ name: "Widget 2", x: 1, y: 0, w: 1, h: 4 },
];
return (
<div style={{ width: "500px" }} ref={parentRef}>
{calculatedWidth && (<ReactGridLayout cols={2} rowHeight={120} width={calculatedWidth}>
{widgetsState.map((widget) => (
<div
key={widget.name}
data-grid={{
x: widget.x,
y: widget.y,
w: widget.w,
h: widget.h,
}}
>
{widget.name}
</div>
))}
</ReactGridLayout>)}
</div>
);
}I simply expected that something like this should be easily possible with the WidthProvider, but I cannot make it work. I'm still open for feedback. I imagine other people would get into this situation too quite often. It's quite a basic requirement. |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Hello everyone!
I'm struggling to figure out what am I doing wrong when setting up this simple react-grid-layout.
What I want to do is to render a react-grid-layout instantly without any transitions. Simply load the page and see the grid directly rendered as per the layout config.
What I see happening is this:
👉 I prepared a very basic example here: https://codesandbox.io/p/sandbox/jlhv9z 👈
Just refresh the iframe and see how the width collapses instantly.
I looked around and found some possible hacks I could still achieve the desired result here
But my problem seems to be with something not working right in
WidthProvider... or... simply me not understanding how to correctly use it.Has anyone encountered this? Did you come up with any good solutions?
I'm open for feedback and suggestions 🙂
Beta Was this translation helpful? Give feedback.
All reactions