Bad Idea? Detect mobile with UserAgent Next.js
I have a component that I am showing/hiding with mediaqueries like this
I was thinking of using getInitialProps to get the UserAgent and detect a mobile device or other and pass down a
isMobile: boolean prop.
New component:
Is this unpredictable/would have some devices falling out of the match above?
Is it just better to do the mediaquery?
7 Replies
change the UI depending on the screen size, not on the useragent. Mobile useragent doesn't necessarily mean that the screen is small.
Yes it’s better to do the media break points. Just take android, there are so many different phones and phone sizes
Unknown User•2y ago
Message Not Public
Sign In & Join Server To View
Also modern apple tablets have the same user agent as safari on desktop, you have to check for features like touch to tell the difference.
Unknown User•2y ago
Message Not Public
Sign In & Join Server To View
You should probably be doing something different for document with touch events and with breakpoints. User agent sniffing should be last chance saloon.
"ontouchstart" in document.documentElement
I would go for feature detection (screen size + touch device)
pointer: coarse is a good css media query