Since window is not available during server-side rendering, you should use a hook that only runs on the client. Here is one way to do it:
Use it like this:
asked about 1 year ago
1
68
I am building a responsive dashboard in Next.js and I want to show a different layout on mobile and desktop. I know that CSS handles responsiveness, but I want to render a completely different component based on screen width in my React component logic. I tried using window.innerWidth but it does not work reliably during SSR. What is the proper way to handle this?
Since window is not available during server-side rendering, you should use a hook that only runs on the client. Here is one way to do it:
Use it like this:
1