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

Function useMediaQuery

src/useMediaQuery.ts:112–140  ·  view source on GitHub ↗
(
  settings: MediaQuerySettings,
  device?: MediaQueryMatchers,
  onChange?: (_: boolean) => void
)

Source from the content-addressed store, hash-verified

110}
111
112const useMediaQuery = (
113 settings: MediaQuerySettings,
114 device?: MediaQueryMatchers,
115 onChange?: (_: boolean) => void
116) => {
117 const deviceSettings = useDevice(device)
118 const query = useQuery(settings)
119 if (!query) throw new Error('Invalid or missing MediaQuery!')
120 const mq = useMatchMedia(query, deviceSettings)
121 const matches = useMatches(mq as unknown as MediaQueryList)
122 const isUpdate = useIsUpdate()
123
124 useEffect(() => {
125 if (isUpdate && onChange) {
126 onChange(matches)
127 }
128 }, [matches])
129
130 useEffect(
131 () => () => {
132 if (mq) {
133 mq.dispose()
134 }
135 },
136 []
137 )
138
139 return matches
140}
141
142export default useMediaQuery

Callers 3

ComponentFunction · 0.85
AppFunction · 0.85
MediaQueryFunction · 0.85

Calls 5

useDeviceFunction · 0.85
useQueryFunction · 0.85
useMatchMediaFunction · 0.85
useMatchesFunction · 0.85
useIsUpdateFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…