Created
August 22, 2020 02:33
-
-
Save diego-miranda-ng/439ffe042cf2e6d6e64bd1c0fb92b3cd to your computer and use it in GitHub Desktop.
Exemplo de um hook personalizado para obter a largura da janela.
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
//useWindowWidth.js | |
import { useLayoutEffect, useState } from "react"; | |
export default function useWindowSize() { | |
const [width, setWidth] = useState(0); | |
useLayoutEffect(() => { | |
const updateWidth = () => setWidth(window.innerWidth); | |
window.addEventListener("resize", updateWidth); | |
updateWidth(); | |
return () => window.removeEventListener("resize", updateWidth); | |
}, []); | |
return width; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment