question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[useMediaQuery] always returns false at the first call

See original GitHub issue

Duplicate of #21048 - [useMediaQuery] always returns false first at page-load

  • The issue is present in the latest release.
  • I have searched the issues of this repository and believe that this is not a duplicate.

Current Behavior 😯

Well, the current behavior is clearly documented

options.noSsr (Boolean [optional]): Defaults to false. In order to perform the server-side rendering reconciliation, it needs to render twice. A first time with nothing and a second time with the children. This double pass rendering cycle comes with a drawback. It’s slower. You can set this flag to true if you are not doing server-side rendering.

However the current behavior is incorrect, and working NOT as documented

Just for context - here is useMediaQuery source - https://github.com/mui-org/material-ui/blob/master/packages/material-ui/src/useMediaQuery/useMediaQuery.js#L44

  const [match, setMatch] = React.useState(() => {
    if (noSsr && supportMatchMedia) {
      // unless noSsr is set, this line would be NEVER called
      return matchMedia(query).matches;
    }
    if (ssrMatchMedia) {
      return ssrMatchMedia(query).matches;
    }
    return defaultMatches;
  });

From this code it’s clear that without noSsr set the match would be always defaultMatches at the first hook call.

While the intention was to keep it default (or ssrMatchMedia) only during hydrate. As a result even CSR only application sufferes from double rendering, which never ends while application is working.

Expected Behavior 🤔

Expected behaviour - useMedia should set correct state just after application start.

  const [match, setMatch] = React.useState(() => {
    if ( (noSsr || pastHydration) && supportMatchMedia) {
      return matchMedia(query).matches;
    }
   ...
  });

However there is a problem of tracking that event, especially with partial hydration in mind, when such event could occur more that once. (not sure MUI supports it)

Technically speaking no change is required to useMediaQuery, but another functionality to provide correct value for noSsr is needed.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:19
  • Comments:23 (10 by maintainers)

github_iconTop GitHub Comments

12reactions
DaniGuardiolacommented, Jul 2, 2020

Hi there! I’m encountering this behavior. I’m not using SSR so I don’t really understand how it influences this, but I’m having useMediaQuery return false in the initial call which results in a nasty flash of CSS. A result of true means mobile, so the page is loading initially with the desktop styles (false), then it instantly switches to the mobile styles). Am I doing something wrong, should I be setting some kind of parameter? Thanks!

7reactions
carlosrsabreucommented, Oct 12, 2022

Can anyone please confirm if this situation is happening again?

Read more comments on GitHub >

github_iconTop Results From Across the Web

[useMediaQuery] always returns false first at page-load #21048
At pageload, useMediaQuery will return false-negative if it was actually true, instantly forcing a re-render and correctly returning true.
Read more >
Why does useMediaQuery always return false in React.js
I'm unfamiliar with react-responsive , but if you're not attached to using that library, MUI provides the same hook and it works perfectly....
Read more >
useMediaQuery - Chakra UI
Keep in mind this API relies on the users browser support of window.matchMedia and will always return false if it is not supported...
Read more >
useMediaQuery - NativeBase
The useMediaQuery hook returns an array of booleans, indicating whether the given query matches or queries match. Why an array? useMediaQuery accepts both...
Read more >
Media queries in React for responsive design - Material UI - MUI
useMediaQuery. This is a CSS media query hook for React. It listens for matches to a CSS media query. It allows the rendering...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found