I am trying to use react-responsive to conditional rendering components base on device width. Everything worked great in client side, but for SSR I followed the documentation to use Context to pass a specific width for initial server render. However, the width that react-responsive received now hard set to the width in Context even if I resize the browser.
Component to define device base on device width:
import { useMediaQuery } from 'react-responsive';
export const Desktop = ({ children }) => {
const isDesktop = useMediaQuery({ minWidth: 801 });
return isDesktop ? children : null;
};
export const Tablet = ({ children }) => {
const isTablet = useMediaQuery({ minWidth: 426, maxWidth: 800 });
return isTablet ? children : null;
};
export const Mobile = ({ children }) => {
const isMobile = useMediaQuery({ maxWidth: 425 });
return isMobile ? children : null;
};
export const MobileTablet = ({ children }) => {
const isMobile = useMediaQuery({ maxWidth: 800 });
return isMobile ? children : null;
};
My use for DeviceIdentifier component:
...
<Desktop>
<CategoryTree />
</Desktop>
...
<MobileTablet>
<BurgerMenu
open={burgerMenuOpen}
onOpenStateChange={setBurgerMenuOpen}
/>
</MobileTablet>
...
Context wrapper in _app.js
import { Context as ResponsiveContext } from 'react-responsive';
...
<ResponsiveContext.Provider value={{ width: 1440 }}>
<Component {...pageProps} />
</ResponsiveContext.Provider>
...
Since I set the width in the context 1440px, my BurgerMenu component is currently never rendered. Anybody have any idea how to make this react-responsive library work in SRR?