How to track window width with using useEffect in react js
import React, { useState, useEffect } from 'react';
const WindowWidthTracker = () => {
const [windowWidth, setWindowWidth] = useState(window.innerWidth);
useEffect(() => {
const handleResize = () => {
setWindowWidth(window.innerWidth);
};
window.addEventListener('resize', handleResize);
return () => {
window.removeEventListener('resize', handleResize);
};
}, []);
return (
<div>
<h1>Current Window Width: {windowWidth}px</h1>
</div>
);
};
export default WindowWidthTracker;
Comments
Post a Comment