react-grid-system
react-grid-system copied to clipboard
useScreenClass not updated after child mounted
Hi,
ScreenClassProvider can be called with useOwnWidth, however this doesn't result in the current screen class until after the window is resized. This is what happens:
-
ScreenClassProvidercallsuseScreenClasswith no source (since the child is not yet mounted), which returns the screen class of the window. -
On the next render
ScreenClassProvidercallsuseScreenClasswith the correct source.useScreenClassthen callsuseStatewithgetScreenClass()as the default value. The screen class is correctly calculated bygetScreenClass, howeveruseStatereturns the previous value since there's no reason for it to change.
In order to work around the issue, I have to force a resize event after my component is mounted:
useEffect(() => { window.dispatchEvent(new Event('resize')); }, []);
This causes useScreenClass to update the screen class.