jaredpalmer / jaredpalmer/the-platform

useMedia flashes wrong state

Open
#83 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
TypeScript
Stars
4.4k
Forks
106
PR merge metrics
No merged PRs in 30d

Description

`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

```js
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?

Contributor guide

No contributing guide indexed for this repository

Research direction

Start at the useMedia hook entry point and reproduce an initial render with defaultMatches to observe the flash. Check the useEffect setup, matchMedia access, and server-side rendering path; done means the initial client state reflects the media query without breaking SSR.

Written by the indexing model from the issue text.

Assessment

Tech stack
react, typescript
Domain
frontend
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.