MCPcopy
hub / github.com/yocontra/react-responsive / useMatchMedia

Function useMatchMedia

src/useMediaQuery.ts:72–92  ·  view source on GitHub ↗
(query: string, device?: MediaQueryMatchers)

Source from the content-addressed store, hash-verified

70}
71
72const useMatchMedia = (query: string, device?: MediaQueryMatchers) => {
73 const getMatchMedia = () => matchMedia(query, device || {}, !!device)
74 const [mq, setMq] = useState(getMatchMedia)
75 const isUpdate = useIsUpdate()
76
77 useEffect(() => {
78 if (isUpdate) {
79 // skip on mounting, it has already been set
80 const newMq = getMatchMedia()
81 setMq(newMq)
82
83 return () => {
84 if (newMq) {
85 newMq.dispose()
86 }
87 }
88 }
89 }, [query, device])
90
91 return mq
92}
93
94const useMatches = (mediaQuery: MediaQueryList): boolean => {
95 const [matches, setMatches] = useState<boolean>(mediaQuery.matches)

Callers 1

useMediaQueryFunction · 0.85

Calls 2

useIsUpdateFunction · 0.85
getMatchMediaFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…