Created
March 26, 2020 15:09
-
-
Save max10rogerio/c4aeaec62178ac56e17454c04e797e44 to your computer and use it in GitHub Desktop.
Hook to verify size screen by param
This file contains hidden or 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 { useEffect, useState } from 'react'; | |
/** | |
* Exemple of sizes: | |
* mobile: '(min-width: 425px)' | |
* tablet: '(min-width: 768px)' | |
* laptop: '(min-width: 1024px)' | |
* @param {String} size | |
* @returns {boolean} | |
*/ | |
export const useMedia = (size) => { | |
const mediaSize = window.matchMedia(size); | |
const getValue = () => { | |
return !mediaSize.matches; | |
}; | |
const [match, setMatch] = useState(getValue); | |
useEffect(() => { | |
const handler = () => setMatch(getValue); | |
mediaSize.addListener(handler); | |
return () => mediaSize.removeListener(handler); | |
}, []); | |
return match; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment