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

How to use useMediaQuery with latest Nextjs #44491

Open
AlexCernik opened this issue Nov 21, 2024 · 1 comment
Open

How to use useMediaQuery with latest Nextjs #44491

AlexCernik opened this issue Nov 21, 2024 · 1 comment
Assignees
Labels

Comments

@AlexCernik
Copy link

AlexCernik commented Nov 21, 2024

Steps to reproduce

page.js

'use client'

const match = useMediaQuery('(max-width:576px)', { noSsr: true });

return (
    <Stack
      component='nav'
      direction='row'
      zIndex={2}
      position='fixed'
      justifyContent='space-between'
      alignItems='center'
      width='100%'
      height={55}
      bgcolor='#0077E6'
      paddingX={2}
      aria-label="Menú principal"
    >
      {!match ?
        <Stack component='ol' direction='row' alignItems='center'>
          <li>
            <Typography
              fontFamily='var(--FW)'
              fontSize={31}
              color='#e6e6e6'
              paddingRight={5}
            >
              G
            </Typography>
          </li>

          {sections.map((item) => (
            <li key={item}>
              <Link
                href={`#${item}`}
                style={{
                  textDecoration: 'none',
                  color: '#ededed',
                  cursor: 'pointer',
                  paddingBlock: 15,
                  paddingInline: 30
                }}
              >
                {item}
              </Link>
            </li>
          ))}
        </Stack>
        :
        <>
          <IconButton onClick={() => toggleDrawer()} style={{ color: '#ededed' }}><IconMenu /></IconButton>
          <Drawer
            anchor='right'
            open={open}
            onClose={() => toggleDrawer()}
            PaperProps={{ sx: { width: 250 } }}
          >
            {sections.map((item) => (
              <Link
                key={item}
                href={`#${item}`}
                style={{
                  textDecoration: 'none',
                  color: 'GrayText',
                  cursor: 'pointer',
                  paddingBlock: 15,
                  paddingInline: 30,
                  marginBlock: 2
                }}
                onClick={() => toggleDrawer()}
              >
                {item}
              </Link>
            ))}
          </Drawer>
        </>
      }
    </Stack>
  );

browser console log

Uncaught Error: Hydration failed because the server rendered HTML didn't match the client. As a result this tree will be regenerated on the client. This can happen if a SSR-ed Client Component used:

- A server/client branch `if (typeof window !== 'undefined')`.
- Variable input such as `Date.now()` or `Math.random()` which changes each time it's called.
- Date formatting in a user's locale which doesn't match the server.
- External changing data without sending a snapshot of it along with the HTML.
- Invalid HTML tag nesting.

It can also happen if the client has a browser extension installed which messes with the HTML before React loaded.

https://react.dev/link/hydration-mismatch

  ...
    <RedirectErrorBoundary router={{...}}>
      <InnerLayoutRouter parallelRouterKey="children" url="/glury" tree={[...]} childNodes={Map} segmentPath={[...]} ...>
        <Page>
          <Box className="__variable...">
            <Styled(div) as="div" ref={null} className="__variable..." theme={{...}} sx={{}}>
              <Insertion>
              <div className="__variable...">
                <Menu>
                  <Grid component="nav" direction="row" zIndex={2} position="fixed" justifyContent="space-between" ...>
                    <MuiStack-root as="nav" ownerState={{...}} ref={null} className="MuiStack-root" ...>
                      <Insertion>
                      <nav className="MuiStack-r..." aria-label="Menú princ...">
                        <IconButton onClick={function onClick} style={{color:"#ed..."}}>
                          <MuiIconButton-root className="MuiIconBut..." centerRipple={true} focusRipple={true} ...>
                            <Insertion>
                            <ButtonBase className="MuiIconBut..." centerRipple={true} focusRipple={true} disabled={false} ...>
                              <MuiButtonBase-root as="button" className="MuiButtonB..." ownerState={{...}} ...>
                                <Insertion>
+                               <button
+                                 className="MuiButtonBase-root MuiIconButton-root MuiIconButton-sizeMedium mui-aixiz9..."
+                                 onBlur={function useEventCallback.useRef}
+                                 onClick={function onClick}
+                                 onContextMenu={function useEventCallback.useRef}
+                                 onFocus={function useEventCallback.useRef}
+                                 onKeyDown={function useEventCallback.useRef}
+                                 onKeyUp={function useEventCallback.useRef}
+                                 onMouseDown={function useEventCallback.useRef}
+                                 onMouseLeave={function useEventCallback.useRef}
+                                 onMouseUp={function useEventCallback.useRef}
+                                 onDragLeave={function useEventCallback.useRef}
+                                 onTouchEnd={function useEventCallback.useRef}
+                                 onTouchMove={function useEventCallback.useRef}
+                                 onTouchStart={function useEventCallback.useRef}
+                                 tabIndex={0}
+                                 type="button"
+                                 disabled={false}
+                                 style={{color:"#ededed"}}
+                                 ref={function useForkRef.useMemo}
+                               >
-                               <ol className="MuiStack-root mui-1d9cypr-MuiStack-root">
                        ...
        ...

Current behavior

No response

Expected behavior

No response

Context

No response

Your environment

No response

Search keywords: useMediaQuery, Nextjs

@AlexCernik AlexCernik added the status: waiting for maintainer These issues haven't been looked at yet by a maintainer label Nov 21, 2024
@siriwatknp
Copy link
Member

Please provide a reproducible sandbox or a repo that I can take a look.

@siriwatknp siriwatknp added status: waiting for author Issue with insufficient information hook: useMediaQuery nextjs and removed status: waiting for maintainer These issues haven't been looked at yet by a maintainer labels Nov 21, 2024
@DiegoAndai DiegoAndai moved this to Backlog in Material UI Nov 21, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
Status: Backlog
Development

No branches or pull requests

2 participants