Skip to content

Instantly share code, notes, and snippets.

@salmanx
Created January 11, 2022 07:37
Show Gist options
  • Save salmanx/923eb57e4552287b5bed9bfb1230f621 to your computer and use it in GitHub Desktop.
Save salmanx/923eb57e4552287b5bed9bfb1230f621 to your computer and use it in GitHub Desktop.
import React, { useEffect, useState } from "react";
export default function WindowResize() {
const [windowWidth, setWindowWidth] = useState(window.innerWidth);
const handleWindowResize = () => setWindowWidth(window.innerWidth)
useEffect(() => {
window.addEventListener('resize', handleWindowResize);
return () => {
window.removeEventListener('resize', handleWindowResize);
}
}, [])
return(
<div>
{windowWidth}
</div>
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment