How to get the height of browser window in React ?

·

1 min read

import "./styles.css";

import { useEffect, useState } from "react";

export default function App() {
  const [windowHeight, setWindowHeight] = useState(window.innerHeight);

  useEffect(() => {
    const handleResize = () => {
      setWindowHeight(window.innerHeight);
    };
    window.addEventListener("resize", handleResize);
    return () => {
      window.removeEventListener("resize", handleResize);
    };
  }, []);

  return (
    <div>
      <h1>{windowHeight}px is the height of window </h1>
    </div>
  );
}