I am trying to set a listener to determine the width of a particular div within my application. If the div is less than 800px wide, I'd like to set the style to display: none. I'm able to do it as is, but it doesn't listen to it and only removes the div if I refresh the page.
const [style, setStyle] = useState({});
const documentRef = useRef(null);
useEffect(() => {
if (documentRef.current.offsetWidth < 800) {
const updatedStyle = {
display: 'none',
}
setStyle(updatedStyle);
}
}, [documentRef.current]);