Closed jack5341 closed 1 year ago
Hey @jack5341, I haven't played much with this lib and next.js or any SSR solution for that matter. Could you post a minimum repro in codesandbox?
The overall issue here is that there's no way for the server to know the size of the user-agent viewport which there's nothing this project can really do to solve. However, the worst case scenario should be that the client-side hydration does a full render. While it's not ideal, it at least should not lead to a broken layout like you're describing.
Hi @kid-icarus
I was read your documentation about react-responsive, but couldn't find any information about my problem without this section https://github.com/contra/react-responsive#server-side-rendering.
Here is my project before i moved responsibility to css. https://github.com/jack5341/invoice-printer/tree/bff83ebaa1bf8cf4318649320f7841ec761ba631
I have the same issue in next-js any solutions?
I was able to fix the issue by disabling ssr on import:
import dynamic from "next/dynamic"
const MediaQuery = dynamic(() => import("react-responsive"), {
ssr: false
})
But how to import useMediaQuery through dynamic const { useMediaQuery } = dynamic(() => import('react-responsive'), { ssr: false, });
this hook does the job for me import { useState, useEffect } from 'react';
export function useMediaQuery(query) { const [matches, setMatches] = useState(false);
useEffect(() => { const media = window.matchMedia(query); if (media.matches !== matches) { setMatches(media.matches); } const listener = () => { setMatches(media.matches); }; media.addListener(listener); return () => media.removeListener(listener); }, [matches, query]);
return matches; }
How we can use useMediaQuery ??
Is the fix for this as simple as returning null/false when window.matchMedia
is unavailable?
I've added more docs about using this library with next.js to the README - I think that resolves this issue, so going to close this for now.
React Responsive doesn't work while on mobile
When i working on next.js project from desktop. I open the mobile preview mode and in there have no problem, but if i refresh my page in preview mode react-responsive doesn't work, thats why in production doesn't work
Example Use;
index.js
upload.js
For better explain;
When i without refresh open preview mode
But if I refresh page in preview mode it looks like this;
My package.json;