I have developed quite a big single page application using create-react-app.
I am in the process of migrating everything to NextJS, mainly for SEO purposes.
I’m scratching my head on one issue : what is the best way to handle responsive design?
In my create-react-app legacy code, I’m always keepings components in sync with window.innerWidth, and am using it to handle most of the responsiveness (except for the grid layout that is handled by material-ui).
But since we can’t guess the client’s width during the server render, then how can you avoid a ‘flicker’ of the UI?
Do we need to delay any responsive UI logic until we can execute on the client?
2
Answers
The solution is :
using javascript to handle responsivness is bad practice, css should be used if we don't want to browser to re-flow the content on the screen.
if you are using material-ui, take a look at this page
This is how I tackled the problem I defined a theme and breakpoints for my stylings. in theme.js file I use createTheme from material-ui. it gives you the option of defining breakpoints:
I predefined sizes I want to change styles based on them. meaning that 0 to 600 is a mobile view…
this is how I use breakpoints in my styles classes
By resizing the window or opening the page in different devices you will see the different styles. It works fine in both ssr and client side components.
note:
If you are not interested in predefining the sizes you can use a different size for each css class like this one.