-
-
Notifications
You must be signed in to change notification settings - Fork 32.5k
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
useMediaQuery return wrong width #16460
Comments
@siriwatknp This is a limitation of the current implementation of the demo. You can find a version that is free from this glitch in #16127 but it relies on a private API. Do you really need this useWidth logic? It's mostly documented for migration purposes, it's not the encourage default approach. |
Actually, I only need a hook that return me the current width of the screen. I thought useWidth is something similar to withWidth. What is your suggestion? |
The glitch is from useWidth logic or it is from useMediaQuery. I think the latter is the reason. |
The glitch comes from the useWidth demo usage. Can you work around the problem with a different usage strategy of |
But I think the glitch is in |
@siriwatknp Nothing surprising from my perspective. I'm not aware of any guarantee in the order the different hooks will resolve. We might end up in an intermediary state where no media query matches. |
Couldn't quite reproduce it though I did get the occasional Wouldn't it make more sense to check if the screen size is above a certain breakpoint and the return the biggest one. For example: function useWidth() {
const theme = useTheme();
const isXs = useMediaQuery(theme.breakpoints.up("xs"));
const isSm = useMediaQuery(theme.breakpoints.up("sm"));
const isMd = useMediaQuery(theme.breakpoints.up("md"));
const isLg = useMediaQuery(theme.breakpoints.up("lg"));
const isXl = useMediaQuery(theme.breakpoints.up("xl"));
if (isXl) return "xl";
if (isLg) return "lg";
if (isMd) return "md";
if (isSm) return "sm";
if (isXs) return "xs";
return null;
} Solves the issue of holes between the intervals which removed the occasional @siriwatknp Could you try that implementation and see if it solves your issue? |
@eps1lon I confirm your proposal, it works better with :) diff --git a/docs/src/pages/components/use-media-query/UseWidth.js b/docs/src/pages/components/use-media-query/UseWidth.js
index b727d4be2..4de165a40 100644
--- a/docs/src/pages/components/use-media-query/UseWidth.js
+++ b/docs/src/pages/components/use-media-query/UseWidth.js
@@ -14,7 +14,7 @@ function useWidth() {
return (
keys.reduce((output, key) => {
// eslint-disable-next-line react-hooks/rules-of-hooks
- const matches = useMediaQuery(theme.breakpoints.only(key));
+ const matches = useMediaQuery(theme.breakpoints.up(key));
return !output && matches ? key : output;
}, null) || 'xs'
); |
@eps1lon yes, that worked! thanks a lot. |
I'm reopening so we don't forget to update the demos. I will give it a go. |
@oliviertassinari if you don't mind, I will update the demo and send a PR |
@siriwatknp Oh, yes, that would be awesome :) |
I found that
useMediaQuery
has some glitch when screen changes from big to smallExpected Behavior 🤔
useMediaQuery
should return correct width when screen changesCurrent Behavior 😯
When the screen change from large to small, there is one event that useMediaQuery return
false
Steps to Reproduce 🕹
try this sandbox, I copy
useWidth
from the doc herelook at the console, stretch screen until you see "md" then narrow it. You will see one "xs".
https://codesandbox.io/s/material-ui-layout-v10-b4wil
The text was updated successfully, but these errors were encountered: