Open mashwishi opened 1 year ago
Just to let you know i have also tried this method
This is my temp solution:
I solved this problem by using useState
, which has false as an initial value, and useEffect
, which calls 'setState(isMobile)' at the time of componentDidMount
.
I think it happens if there is a difference between the initial rendering result of client-side and the server-side rendering result.
@mashwishi have you found any better workaround for this?
@mashwishi have you found any better workaround for this?
No, I posted my temp solution
I found answer on stack overflow. here
Make component server component.
import { getSelectorsByUserAgent } from "react-device-detect"
import { headers } from "next/headers"
const { isMobile } = getSelectorsByUserAgent(
headers().get("user-agent") ?? ""
)
return (
<>
{(isMobile) ?
<nav>Nabar Mobile</nav>
:
(
<h1>This is rendered only on desktop</h1>
)
}
</>
);
I found answer on stack overflow. here
Make component server component.
import { getSelectorsByUserAgent } from "react-device-detect" import { headers } from "next/headers" const { isMobile } = getSelectorsByUserAgent( headers().get("user-agent") ?? "" ) return ( <> {(isMobile) ? <nav>Nabar Mobile</nav> : ( <h1>This is rendered only on desktop</h1> ) } </> );
@nikhil647 I have tried this but for tablet devices it's detecting as desktop.
Bug description
Steps to reproduce
Device user-agent Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36 Edg/114.0.1823.82
Device/Browser type/name Windows 11 Pro 64Bit - Microsoft Edge
Code
Issue: