Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

useMediaQuery react 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

useMediaquery hook react

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;
Comment

useMediaquery hook react

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;
Comment

useMediaquery hook react

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;
Comment

PREVIOUS NEXT
Code Example
Javascript :: JavaScript - How to get the extension of a filename 
Javascript :: File is a CommonJS; it may be converted to an ES6 module 
Javascript :: json schema array of objects 
Javascript :: nodejs readdir 
Javascript :: filter array of objects to remove duplicate keys 
Javascript :: play music from file js 
Javascript :: store array in localstorage 
Javascript :: jquery observe class change 
Javascript :: import jsx file without extension 
Javascript :: html add new line in js alert 
Javascript :: discord.js bot 
Javascript :: javascript compare two objects 
Javascript :: jquery get location of user 
Javascript :: javascript load multiple images 
Javascript :: how to find the last item in an array 
Javascript :: react native android run 
Javascript :: foreach object js 
Javascript :: js omit last string 
Javascript :: link vs NavLink in React Router 
Javascript :: Node Sass could not find a binding for your current environment 
Javascript :: js test if string 
Javascript :: convert utc to date javascript 
Javascript :: how to pass props in react test cases 
Javascript :: jquery get unique values from array 
Javascript :: js string startswith ignore case 
Javascript :: how to return 5 records instead of 10 records in datatable 
Javascript :: javascript camera 
Javascript :: firebase react router page not found on page refresh 
Javascript :: min of an array javascript 
Javascript :: bootstrap multiselect change value 
ADD CONTENT
Topic
Content
Source link
Name
1+5 =