Skip to content

Does it provide a variable to say whether it's on mobile(phone) or PC ? #5858

Closed Answered by Josh-Cena
Chasen-Zhang asked this question in General
Discussion options

You must be logged in to vote

There's a @theme/hooks/useWindowSize hook which you can use in your React app. useWindowSize() returns desktop, mobile, or ssr if it is server-side rendered. We use it ourselves:

const windowSize = useWindowSize();
// Desktop sidebar visible on hydration: need SSR rendering
const shouldRenderSidebarDesktop =
windowSize === 'desktop' || windowSize === 'ssr';
// Mobile sidebar not visible on hydration: can avoid SSR rendering
const shouldRenderSidebarMobile = windowSize === 'mobile';
return (
<>
{shouldRenderSidebarDesktop &&

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by Josh-Cena
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
2 participants