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

useScreenClass not updated after child mounted #160

Open
timothyarmes opened this issue Oct 8, 2020 · 0 comments
Open

useScreenClass not updated after child mounted #160

timothyarmes opened this issue Oct 8, 2020 · 0 comments

Comments

@timothyarmes
Copy link

timothyarmes commented Oct 8, 2020

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:

  1. ScreenClassProvider calls useScreenClass with no source (since the child is not yet mounted), which returns the screen class of the window.

  2. On the next render ScreenClassProvider calls useScreenClass with the correct source. useScreenClass then calls useState with getScreenClass() as the default value. The screen class is correctly calculated by getScreenClass, however useState returns 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant