0

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]);
Derek Wang
  • 10,098
  • 4
  • 18
  • 39
Nick
  • 1,471
  • 2
  • 21
  • 35
  • Not sure, but [this](https://stackoverflow.com/questions/34407186/how-to-detect-html-div-width-and-height-once-div-is-done-resizing) might help. – Debargha Roy Oct 19 '20 at 16:20

1 Answers1

0

You can use CSS media query to hide something when below 800 px.

@media only screen and (max-width: 800px) {
  show-lg-only {
    display: none;
  }
}
Aadil Mehraj
  • 2,586
  • 1
  • 7
  • 17