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

Popular posts from this blog

Create a Connection in Node js and make an API

React JS | Store form data in localStorage

React Redux Toolkit