Skip to content

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

@Akuukis

Description

@Akuukis

At pageload, useMediaQuery will return false-negative if it was actually true, instantly forcing a re-render and correctly returning true.

  • 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 😯

There are two ways to formulate it:

  • At pageload, useMediaQuery returns false first. Then, if they actually were true from the very beggining, they will force instant re-render, returning true.
  • At pageload, useMediaQuery will return false-negative if it was actually true, instantly forcing a re-render correctly returning true afterwards.

That leads to potential double-renders at page-load, impacting performance.

Expected Behavior 🤔

useMediaQuery to return correct value with first time.

Steps to Reproduce 🕹

https://codesandbox.io/s/lively-sky-gl983

Context 🔦

I have a wrapper component that renders either A or B subcomponent based on a media querry, where both A and B are both rather heavy. With this problem, I can't ensure that I will render the right one first time, without wasting time rendering the other.

Your Environment 🌎

See versions on https://codesandbox.io

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions