Skip to content

How do I use useMediaQuery only on the client when using NextJS? #3437

Answered by turtletongue
turtletongue asked this question in Q&A
Discussion options

You must be logged in to vote

@mercurymirror you don't need to use dynamic for useMediaQuery. To solve this problem I used dynamic import for component where useMediaQuery was called.

example-component.tsx

import { useMediaQuery } from '@chakra-ui/react';

const ExampleComponent = () => {
    const [isLessThan800] = useMediaQuery("(max-width: 800px)");

    ...
};

page.tsx

import dynamic from 'next/dynamic';

const ExampleComponent = dynamic(
  () => import('./example-component'),
  { ssr: false }
);

...

But I don't think now that using useMediaQuery is a good idea with NextJS. I think it's better to use Chakra layouts such as flex and grid to make your website responsive because you'll have advantages of server side…

Replies: 1 comment 4 replies

Comment options

You must be logged in to vote
4 replies
@mercurymirror
Comment options

@turtletongue
Comment options

@mercurymirror
Comment options

@turtletongue
Comment options

Answer selected by turtletongue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants