I have a dashboard panel using react js that I used react-grid-layout in it; I used react-grid-layout in my content and that works correctly but when I close my right side or left side panels (drawers) , the width of my content that is the parent of my react-grid-layout modifies but after this changes my column's width did not modify according to their parent's width size; How can I reload my ResponsiveGridLayout component to changing the childs(columns) width? this is simple code for showing my problem in this question : example
2 Answers
If you look at the documentation here you'll see that ResponsiveGridLayout
is a combination of a component called Responsive
and a HOC called WidthProvider
import { Responsive, WidthProvider } from 'react-grid-layout';
const ResponsiveGridLayout = WidthProvider(Responsive);
If you look at the code of WidthProvider
you may notice it subscribes to widnow resize event
window.addEventListener("resize", this.onWindowResize);
which isn't particularly useful for you, because in your case window does not resize. One way to deal with this problem is to create your own WidthProvider
and attach the listener to the element that actually resizes and wrap it around Responsive
const MyVeryOwnResponsiveGridLayout = MyWidthProvider(Responsive);
P.S. You can try requesting the feature from the component creator or volunteer to contribute to the project

- 4,916
- 1
- 15
- 17
-
Thank you very much for your answer @NadiaChibrikova ,Exactly, I read a lot its documents and did not find an answer to my problem. Thank you for your solution. Can you please help me how to make a provider? – N.SH Jan 04 '21 at 05:20
-
1@N.SH they didn't leave much room for extension unfortunately, if you want to stick to this library, you will have to copy WidthProvider, add componentDidUpdate to the component and call onWindowResize from it. Then you can update component's props when sidebar is shown/hidden, React will call componentDidUpdate and your grid will get its new width – Nadia Chibrikova Jan 04 '21 at 13:11
-
Thank you so much @NadiaChibrikova , I'll definitely try your solution – N.SH Jan 06 '21 at 05:23
-
@N.SH did you find an answer for that ? – mlisthenewcool May 18 '21 at 09:18
-
@mlisthenewcool I create an issue for this problem in github this can help you https://github.com/react-grid-layout/react-grid-layout/issues/1369#event-4545519610 – N.SH May 25 '21 at 12:26
I found a better answer than extending the WidthProvider:
Trigger a Window Resize event (without actually resizing the window). I found a delay of 1ms is necessary to make it work.
See this solution: https://stackoverflow.com/a/62874552/17570516

- 1
- 1
- 2