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 :: nodejs mysql Getting the number of affected rows 
Javascript :: node js orderby method 
Javascript :: extract image in p5.js 
Javascript :: wast node 
Javascript :: searchable 
Javascript :: js delete without changing index 
Javascript :: ajax each 
Javascript :: react-folder tree example 
Javascript :: absolute item onPress ToucableOpacity problem 
Javascript :: get object property dynamically liquid 
Javascript :: get id of click element within a class list jquery 
Javascript :: javascript axios response.data.pipe not a function 
Javascript :: Ant Media Filter Plugin for Text 
Javascript :: angularjs How to set code view as deafult instead of tree in jsoneditor 
Javascript :: DeepCopy in Angularjs 
Javascript :: Angular Frontend - How do I change a value I got from backend in frontend 
Javascript :: Angular Nx Nrwl - Cannot parse tsconfig.base.json: PropertyNameExpected in JSON when try to create a new lib 
Javascript :: javascript unique grouped arrays 
Javascript :: remove symbols from cnpj js 
Javascript :: setup app files in node js 
Javascript :: Transfer file to server using rsync 
Javascript :: filter number from string in javascript 
Javascript :: iterate over element parent jquery 
Javascript :: vanillaJS add elements to body 
Javascript :: vimscript replace function 
Javascript :: A Nodule Module For ExpressJS 
Javascript :: adding amplify in index.js react native 
Javascript :: let result = 7 + 13 / 9 + 7; let result2 = 100 / 2 * 6; answer= result* result2; result = answer; final Result = result.toFixed(2); final Number = Number(final Result); console.log(finalNumber); 
Javascript :: check if can go back react native 
Javascript :: Enqueue jquery for TypeError: $.browser is undefined issue 
ADD CONTENT
Topic
Content
Source link
Name
2+4 =