Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

useMediaQuery in Next.js produces "Warning: Expected server HTML to contain a matching <div> in <div>." #316

Closed
ldrick opened this issue Oct 23, 2021 · 3 comments
Labels
bug Something isn't working

Comments

@ldrick
Copy link

ldrick commented Oct 23, 2021

Describe the bug
If you use in a Next.js Application useMedaQuery it results in the same issue described here:
vercel/next.js#17443

To Reproduce
Steps to reproduce the behavior:

  1. Use Next.js Application and implement useMedaQuery in one Component
  2. Load the dev environment of that Application
  3. Open Browser Console ( any current Firefox or CHrome will do)
  4. Press F5 in Browser to reload
  5. See error in console

Expected behavior
No error is thrown, as it further breaks reloading CSS in Next.js Dev Application and probably more.

Additional context
Following this amazing description: https://dev.to/adrien/creating-a-custom-react-hook-to-get-the-window-s-dimensions-in-next-js-135k it works for me like this right now:

import { useEffect, useState } from 'react';

const useMediaQuery = (mediaQuery: string): boolean => {
  const [isVerified, setIsVerified] = useState(false);

  useEffect(() => {
    const mediaQueryList = window.matchMedia(mediaQuery);
    const documentChangeHandler = (): void => setIsVerified(!!mediaQueryList.matches);

    try {
      mediaQueryList.addEventListener('change', documentChangeHandler);
    } catch (e) {
      // Safari isn't supporting mediaQueryList.addEventListener
      mediaQueryList.addListener(documentChangeHandler);
    }

    documentChangeHandler();

    return (): void => {
      try {
        mediaQueryList.removeEventListener('change', documentChangeHandler);
      } catch (e) {
        // Safari isn't supporting mediaQueryList.removeEventListener
        mediaQueryList.removeListener(documentChangeHandler);
      }
    };
    // eslint-disable-next-line react-hooks/exhaustive-deps
  }, []); // Empty array ensures that effect is only run on mount

  return isVerified;
};

export { useMediaQuery };
@antonioru antonioru added the bug Something isn't working label Jun 10, 2022
@antonioru
Copy link
Owner

Hi @ldrick sorry for the very late reply

I think you're right, that's a bug... would you mind to open a PR?

@playerony
Copy link
Contributor

Does this error still occur for you? Because I've tested this on my app with next.js version 12.1.6 and I can't reproduce this.

@antonioru
Copy link
Owner

@playerony @ldrick

I recently tested this on Next.js and same as Pawel I haven't got any error/message...
I'm closing this for now, thanks guys

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants