Replies: 4 comments 2 replies
-
Your initial state tries to access the window object, |
Beta Was this translation helpful? Give feedback.
-
As said before, the problem here is that you are doing unguarded DOM/Window access, and when this code runs on the server, the absence of window makes it crash. A more resilient hook would be: export const useWindowSize = () => {
const [windowSize, setWindowSize] = useState(0);
useEffect(() => {
// inside useEffect, the window is always present
const updateWindowSize = () => {
setWindowSize(window.innerWidth);
};
updateWindowSize(); // as soon as we are on the client, run this handler
window.addEventListener('resize', updateWindowSize); // works only on resize events
return () => {
window.removeEventListener('resize', updateWindowSize); // clean up
};
}, []); // attach this once
return windowSize;
}; |
Beta Was this translation helpful? Give feedback.
-
AGAIN. he said to initiliaze with 0. Works for me. Make sure you set state of windowSize inside useEffect() |
Beta Was this translation helpful? Give feedback.
-
The issue was because the leaflet library was using SSR instead of client, resulting in the error 'window is not defined'. I solved this by importing dynamic from nextjs. Please see the below screenshot |
Beta Was this translation helpful? Give feedback.
-
Hey
This is my hook:
When i want use in page this hook, i get:
Beta Was this translation helpful? Give feedback.
All reactions