Created
February 13, 2019 06:21
-
-
Save SanichKotikov/e49a377aefa73cccac2e072f2723c537 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { useState, useEffect } from 'react'; | |
const DELAY = 400; | |
const mapQueries = (query: string) => window.matchMedia(query).matches; | |
function useQueries(queries: string[]): boolean[] { | |
const [values, setValues] = useState(queries.map(mapQueries)); | |
useEffect(() => { | |
let timer: number; | |
const onResize = () => { | |
window.clearTimeout(timer); | |
timer = window.setTimeout(() => setValues(queries.map(mapQueries)), DELAY); | |
}; | |
window.addEventListener('resize', onResize); | |
return () => { | |
window.clearTimeout(timer); | |
window.removeEventListener('resize', onResize); | |
}; | |
}, []); | |
return values; | |
} | |
export default useQueries; | |
// const [xs, sm] = useQueries([`(min-width: ${SCREENS.xs}px)`, `(min-width: ${SCREENS.sm}px)`]); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment