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

useMedia flashes wrong state #83

Open
dreyks opened this issue Nov 3, 2019 · 0 comments
Open

useMedia flashes wrong state #83

dreyks opened this issue Nov 3, 2019 · 0 comments

Comments

@dreyks
Copy link

dreyks commented Nov 3, 2019

defaultMatches bleeds through the initial render resulting in flashes of wrong state. replacing useEffect with useLayoutEffect would fix this but would require a workaround for server-side rendering

alternatively something like this could work

const mediaQueryList = useRef()
const getMediaQueryList = () => window.matchMedia(
  typeof query === 'string' ? query : json2mq(query)
);
let initialMatches;
if (typeof window !== 'undefined') {
  mediaQueryList.current = getMediaQueryList();
  initialMatches = mediaQueryList.current.matches;
}

initialMatches = typeof initialMatches !== 'undefined' ? initialMatches : defaultMatches;
const [matches, setMatches] = useState(initialMatches);

useEffect(
  () => {
    mediaQueryList.current = mediaQueryList.current || getMediaQueryList()
    // the rest of the method
  },
  [query]
)

what do you think?

kingcons added a commit to kingcons/the-platform that referenced this issue Jan 10, 2020
jaredpalmer pushed a commit that referenced this issue Nov 10, 2020
Useful for working around Issue #83
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