Get window width on server
Hi all! Please forgive me if this is a silly question. I am relatively new to SSR (via react at least) so I am struggling to do this properly. I have a component that needs to measure the window size to determine what border radius to use. Obviously there is no
window on the server so I have to wait until the component renders on the client. But, I am getting a flash of incorrect style on the client before I measure the window and set the correct value. Is there a way to get window width (or at least browser type, etc) on the server. I started of with a useEffect solution and have now moved to a useState solution.
The above code has sm = false on the first render and then sets sm = true after. So there is a flash of wrongly styled content. What is the proper way to solve this?
Do I need to return null until I have the data I need?
1 Reply
like-goldOP•13mo ago
Actually, that may affect SSR right? Rather than return
null I should probably use display: mounted ? undefined : 'none'