File tree 1 file changed +7
-8
lines changed
packages/web-api-hooks/src
1 file changed +7
-8
lines changed Original file line number Diff line number Diff line change 1
- import { useEffect , useMemo , useState } from 'react' ;
1
+ import { useEffect , useState } from 'react' ;
2
2
import { canUseDOM } from './utils' ;
3
3
4
4
/**
@@ -15,24 +15,23 @@ import { canUseDOM } from './utils';
15
15
* }
16
16
*/
17
17
export default function useMedia ( query : string ) : boolean {
18
- const mediaQueryList = useMemo ( ( ) => canUseDOM && matchMedia ( query ) , [ query ] ) ;
19
- const [ matches , setMatches ] = useState (
20
- mediaQueryList && mediaQueryList . matches ,
18
+ const [ matches , setMatches ] = useState ( ( ) =>
19
+ canUseDOM ? matchMedia ( query ) . matches : false ,
21
20
) ;
22
21
23
22
useEffect ( ( ) => {
24
- if ( ! mediaQueryList ) return undefined ;
23
+ const mediaQueryList = matchMedia ( query ) ;
25
24
26
- function handleChange ( event : MediaQueryListEvent ) {
27
- setMatches ( event . matches ) ;
25
+ function handleChange ( ) {
26
+ setMatches ( mediaQueryList . matches ) ;
28
27
}
29
28
30
29
// TODO: Refactor to `managedEventListener` when `change` event is supported
31
30
mediaQueryList . addListener ( handleChange ) ;
32
31
return ( ) => {
33
32
mediaQueryList . removeListener ( handleChange ) ;
34
33
} ;
35
- } , [ mediaQueryList ] ) ;
34
+ } , [ query ] ) ;
36
35
37
36
return matches ;
38
37
}
You can’t perform that action at this time.
0 commit comments