import React, { useState, useEffect } from 'react';
const UseEffectCleanup = () => {
const [width, setWidth] = useState(window.innerWidth);
const checkSize = () => {
setWidth(window.innerWidth)
}
useEffect(() => {
window.addEventListener('resize', checkSize)
return () => {
window.removeEventListener('resize', checkSize)
}
}, [width]);
return (
<>
<h1>Window Size</h1>
<h2>{`${width}px`}</h2>
</>
);
}
export default UseEffectCleanup;
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter