I have been trying to implement a way for rendering components depending on the screen size. At the moment it works, however we are getting some errors in the console.
The errors are related to the server and the client having different output.
This of course happens because on the server we cannot know the screen size so we the queries basically dont work.
So, we have a components like this:
const SmallOnly = ({ children }) => {
const smallOnly = useMediaQuery({ query: '(max-width: 39.9375em)' })
return smallOnly ? children : null
}
This will allow us to render elements only on small screens. useMediaQuery
comes from a package called react-responsive
.
So this component will be used like this:
<LeftHeader>
<SmallOnly>{shoppingCartIcon}</SmallOnly>
<Medium>{logo}</Medium>
</LeftHeader>
Which causes the following error:
Warning: Expected server HTML to contain a matching <section> in <div>.
section
StyledTopBar
TopBar
Medium
Header
Theme
Component
App
FrontityApp
So for me its clear why this error happens. Different HTML on server and client causes this, however I cannot figure out if there is any way around it.
Does anyone have any ideas how we can make this work, without causing errors?
Looking forward to hearing if there are some nice ideas around this.