get window object in nextjs

  1. Import the useEffect and useState hooks from the 'react' module.
import { useEffect, useState } from 'react';
  1. Define a functional component that will be used to render your Next.js page.
const YourPage = () => {
  // Your component logic goes here
}
  1. Inside the component, declare a state variable to hold the window object.
const [windowObject, setWindowObject] = useState(null);
  1. Use the useEffect hook to update the state variable with the window object when the component mounts.
useEffect(() => {
  setWindowObject(window);
}, []);
  1. Access the windowObject variable in your component as needed.
console.log(windowObject);
  1. Complete your functional component.
const YourPage = () => {
  const [windowObject, setWindowObject] = useState(null);

  useEffect(() => {
    setWindowObject(window);
  }, []);

  console.log(windowObject);

  // Your component logic goes here

  return (
    // Your JSX for rendering the component
  );
};
  1. Make sure to export your component at the end of the file if it's not exported already.
export default YourPage;