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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

[useMediaQuery] Doesn't fire after state change #36165

Open
2 tasks done
pavsidhu opened this issue Feb 13, 2023 · 9 comments
Open
2 tasks done

[useMediaQuery] Doesn't fire after state change #36165

pavsidhu opened this issue Feb 13, 2023 · 9 comments
Labels
bug 馃悰 Something doesn't work external dependency Blocked by external dependency, we can鈥檛 do anything about it hook: useMediaQuery

Comments

@pavsidhu
Copy link

Duplicates

  • I have searched the existing issues

Latest version

  • I have tested the latest version

Steps to reproduce 馃暪

Link to live example: https://codesandbox.io/s/charming-microservice-imyon7?file=/src/App.tsx

Steps:

  1. Increase and decrease the window width so you can see the media query updates correctly in the console
  2. Resize the screen to larger than 600px width and press the button, this will fire the if statement and update the state of isActive
  3. Now try resizing. the window width above and below 600px

Current behavior 馃槸

The component only rerenders when resizing the window when the media query does not match

Expected behavior 馃

I expect the component to also rerender when the media query matches

Context 馃敠

No response

Your environment 馃寧

No response

@pavsidhu pavsidhu added the status: waiting for maintainer These issues haven't been looked at yet by a maintainer label Feb 13, 2023
@oliviertassinari
Copy link
Member

oliviertassinari commented Feb 14, 2023

I don't see a bug, it seems to behave correctly. How is it not correct? Do you have a recording?

@pavsidhu
Copy link
Author

Hopefully this video helps:

Screen.Recording.2023-02-14.at.14.41.14.mov

Do you see how the component doesn't rerender after the button is pressed?

@michaldudak
Copy link
Member

I'm not able to reproduce it. @pavsidhu, could you please share details about your environment?

@michaldudak michaldudak added status: waiting for author Issue with insufficient information and removed status: waiting for maintainer These issues haven't been looked at yet by a maintainer labels Feb 16, 2023
@pavsidhu
Copy link
Author

Sure, the browser used in that example was Microsoft Edge Version 110 running on a M1 Macbook Pro Ventura 13.0.1

@github-actions github-actions bot removed the status: waiting for author Issue with insufficient information label Feb 16, 2023
@michaldudak
Copy link
Member

I couldn't reproduce it on Edge (I've got v111), but it happened once in Safari. I couldn't repeat it, though. It seems there is a subtle bug somewhere.

@michaldudak michaldudak added the bug 馃悰 Something doesn't work label Feb 16, 2023
@zhengjitf
Copy link

zhengjitf commented Mar 1, 2023

Steps:

  1. Increase and decrease the window width so you can see the media query updates correctly in the console
  2. Resize the screen to larger than 600px width and press the button, this will fire the if statement and update the state of isActive
  3. Now try resizing. the window width above and below 600px

For these steps, I couldn't reproduce the issue. But I can reproduce the issue following the steps of the given video.

Steps:

  1. Increase and decrease the window width so you can see the media query updates correctly in the console
  2. Resize the screen to less than 600px width and press the button, then resize the screen to larger than 600px
  3. Now try resizing. the window width above and below 600px, the component will just rerender when the media query matches

It should be the problem with useSyncExternalStore. I found something by debugging the given example. Finally, I found that this issue has been fixed by Fix useSyncExternalStore dropped update when the state is dispatched in render phase but hasn't been released to the latest stable version. So maybe need to wait for the next react stable version?

PS: I only change react version to 18.3.0-next-d49e0e0be-20230302, it works well, see the revised example.

@michaldudak
Copy link
Member

Thanks for investigating it. Let's wait for 18.3, then. I'm labelling it as "external dependency".

@michaldudak michaldudak added the external dependency Blocked by external dependency, we can鈥檛 do anything about it label Mar 16, 2023
@michaldudak michaldudak removed their assignment Mar 16, 2023
@michaldudak michaldudak changed the title useMediaQuery doesn't fire after state change [useMediaQuery] Doesn't fire after state change Mar 16, 2023
@MaltesBytes
Copy link

+1

@kgregory
Copy link
Member

kgregory commented May 14, 2024

This hasn't been addressed in 18.3.1 but is fixed in the 18.3.0 canary. Does anyone know if there's another React 18 version coming?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug 馃悰 Something doesn't work external dependency Blocked by external dependency, we can鈥檛 do anything about it hook: useMediaQuery
Projects
None yet
Development

No branches or pull requests

7 participants