Page Menu
Home
c4science
Search
Configure Global Search
Log In
Files
F123427385
useMediaQuery.js
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Subscribers
None
File Metadata
Details
File Info
Storage
Attached
Created
Sun, Jul 27, 07:58
Size
1005 B
Mime Type
text/x-java
Expires
Tue, Jul 29, 07:58 (2 d)
Engine
blob
Format
Raw Data
Handle
27671215
Attached To
rOACCT Open Access Compliance Check Tool (OACCT)
useMediaQuery.js
View Options
import
React
from
'react'
;
export
default
function
useMediaQuery
(
query
)
{
// Keep track of the preference in state, start with the current match
var
_React$useState
=
React
.
useState
(
function
()
{
if
(
typeof
window
!==
'undefined'
)
{
return
window
.
matchMedia
&&
window
.
matchMedia
(
query
).
matches
;
}
}),
isMatch
=
_React$useState
[
0
],
setIsMatch
=
_React$useState
[
1
];
// Watch for changes
React
.
useEffect
(
function
()
{
if
(
typeof
window
!==
'undefined'
)
{
if
(
!
window
.
matchMedia
)
{
return
;
}
// Create a matcher
var
matcher
=
window
.
matchMedia
(
query
);
// Create our handler
var
onChange
=
function
onChange
(
_ref
)
{
var
matches
=
_ref
.
matches
;
return
setIsMatch
(
matches
);
};
// Listen for changes
matcher
.
addListener
(
onChange
);
return
function
()
{
// Stop listening for changes
matcher
.
removeListener
(
onChange
);
};
}
},
[
isMatch
,
query
,
setIsMatch
]);
return
isMatch
;
}
Event Timeline
Log In to Comment