MCPcopy Index your code
hub / github.com/github/docs / useMediaQuery

Function useMediaQuery

components/hooks/useMediaQuery.ts:3–28  ·  view source on GitHub ↗
(query: string)

Source from the content-addressed store, hash-verified

1import { useState, useEffect } from 'react'
2
3export function useMediaQuery(query: string) {
4 const [state, setState] = useState(
5 typeof window !== 'undefined' ? window.matchMedia(query).matches : false
6 )
7
8 useEffect(() => {
9 let mounted = true
10 const mql = window.matchMedia(query)
11 const onChange = () => {
12 if (!mounted) {
13 return
14 }
15 setState(!!mql.matches)
16 }
17
18 mql.addEventListener('change', onChange)
19 setState(mql.matches)
20
21 return () => {
22 mounted = false
23 mql.removeEventListener('change', onChange)
24 }
25 }, [query])
26
27 return state
28}

Callers 1

useBreakpointFunction · 0.90

Calls

no outgoing calls

Tested by

no test coverage detected