Skip to content

Instantly share code, notes, and snippets.

@diego-miranda-ng
Created August 22, 2020 02:33
Show Gist options
  • Save diego-miranda-ng/439ffe042cf2e6d6e64bd1c0fb92b3cd to your computer and use it in GitHub Desktop.
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.
//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