usewindowsize hook in nextjs

The solution for “usewindowsize hook in nextjs” can be found here. The following code will assist you in solving the problem.

import { useEffect, useState } from ‘react’

// Hook
export default function useWindowSize() {
const [windowSize, setWindowSize] = useState({
width: undefined,
height: undefined,
useEffect(() => {
// Handler to call on window resize
function handleResize() {
// Set window width/height to state
width: window.innerWidth,
height: window.innerHeight,
// Add event listener
window.addEventListener(‘resize’, handleResize)
// Call handler right away so state gets updated with initial window size
// Remove event listener on cleanup
return () => window.removeEventListener(‘resize’, handleResize)
}, []) // Empty array ensures that effect is only run on mount
return windowSize

Thank you for using DeclareCode; We hope you were able to resolve the issue.

More questions on [categories-list]

inline scripts encapsulated in