Skip to content

Instantly share code, notes, and snippets.

@SanichKotikov
Created February 13, 2019 06:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SanichKotikov/e49a377aefa73cccac2e072f2723c537 to your computer and use it in GitHub Desktop.
Save SanichKotikov/e49a377aefa73cccac2e072f2723c537 to your computer and use it in GitHub Desktop.
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