Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

react media query hook

import { useState, useEffect } from "react";

const useMediaQuery = (query) => {
  const [matches, setMatches] = useState(false);

  useEffect(() => {
    const media = window.matchMedia(query);
    if (media.matches !== matches) {
      setMatches(media.matches);
    }
    const listener = () => setMatches(media.matches);
    window.addEventListener("resize", listener);
    return () => window.removeEventListener("resize", listener);
  }, [matches, query]);

  return matches;
}

export default useMediaQuery;

// using the hook
const isDesktop = useMediaQuery('(min-width: 960px)');
Comment

PREVIOUS NEXT
Code Example
Javascript :: javascript combine array of arrays 
Javascript :: display am pm in javascript 
Javascript :: react get current date yyyy-mm-dd 
Javascript :: fs.readdir example 
Javascript :: javascript remove first character from string 
Javascript :: react overflow scroll 
Javascript :: local storage angular 
Javascript :: drupal 8 get node from path alias 
Javascript :: graphql float 
Javascript :: express.json 
Javascript :: javascript map 2 array of objects 
Javascript :: js create element from string 
Javascript :: how to extract year from utc in javascript 
Javascript :: vuex v-model 
Javascript :: how to get last element of array 
Javascript :: react native run android 
Javascript :: js multiply string 
Javascript :: javascript decimal to string 
Javascript :: check if over 18 javascript 
Javascript :: vuejs set 
Javascript :: react state add to array 
Javascript :: javascipt get last element of array 
Javascript :: add leading spaced in string javascript 
Javascript :: javascript reverse array 
Javascript :: a <route is only ever to be used as the child of <routes element" 
Javascript :: how to return 5 records instead of 10 records in datatable in laravel 
Javascript :: check if number is single digit javascript 
Javascript :: js enter key event listener 
Javascript :: get child eleemtn by native element angular 
Javascript :: string remove accents 
ADD CONTENT
Topic
Content
Source link
Name
7+4 =