- Import the
useEffect
and useState
hooks from the 'react' module.
import { useEffect, useState } from 'react';
- Define a functional component that will be used to render your Next.js page.
const YourPage = () => {
// Your component logic goes here
}
- Inside the component, declare a state variable to hold the window object.
const [windowObject, setWindowObject] = useState(null);
- Use the
useEffect
hook to update the state variable with the window
object when the component mounts.
useEffect(() => {
setWindowObject(window);
}, []);
- Access the
windowObject
variable in your component as needed.
console.log(windowObject);
- 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
);
};
- Make sure to export your component at the end of the file if it's not exported already.
export default YourPage;