mui / mui-x

MUI X: Build complex and data-rich applications using a growing list of advanced React components, like the Data Grid, Date and Time Pickers, Charts, and more!
https://mui.com/x/
4.02k stars 1.24k forks source link

[pickers][DateCalendar] Hydration Issue #13475

Closed MyNameIsTakenOMG closed 2 months ago

MyNameIsTakenOMG commented 2 months ago

Steps to reproduce

Link to live example: (required)

Steps:

  1. clone the sample repo: https://github.com/MyNameIsTakenOMG/test-date-calendar.git
  2. install the dependencies: npm i
  3. run the server npm run dev, then check out the route: localhost:3000/booking

Current behavior

Currently, when using DateCalendar component, it will cause Hydration Issue, and I also found a similar issue here #7439. Please take a look at screenshots below:

Screenshot 2024-06-13 at 3 09 57 PM Screenshot 2024-06-13 at 3 11 44 PM

Expected behavior

should be no error

Context

No response

Your environment

npx @mui/envinfo ``` Browser: Google Chrome System: OS: macOS 14.5 Binaries: Node: 21.7.1 - /opt/homebrew/bin/node npm: 10.5.0 - /opt/homebrew/bin/npm pnpm: Not Found Browsers: Chrome: 126.0.6478.56 Edge: Not Found Safari: 17.5 npmPackages: @emotion/react: ^11.11.4 => 11.11.4 @emotion/styled: ^11.11.5 => 11.11.5 @mui/base: 5.0.0-beta.40 @mui/core-downloads-tracker: 5.15.20 @mui/material: ^5.15.20 => 5.15.20 @mui/private-theming: 5.15.20 @mui/styled-engine: 5.15.14 @mui/system: 5.15.20 @mui/types: 7.2.14 @mui/utils: 5.15.20 @mui/x-date-pickers: ^7.7.0 => 7.7.0 @types/react: ^18 => 18.3.3 react: ^18 => 18.3.1 react-dom: ^18 => 18.3.1 typescript: ^5 => 5.4.5 ```

Search keywords: DateCalendar

github-actions[bot] commented 2 months ago

:warning: This issue has been closed. If you have a similar problem but not exactly the same, please open a new issue. Now, if you have additional information related to this issue or things that could help future readers, feel free to leave a comment.

@MyNameIsTakenOMG: How did we do? Your experience with our support team matters to us. If you have a moment, please share your thoughts in this short Support Satisfaction survey.

MyNameIsTakenOMG commented 2 months ago

In my case, when I visited the route localhost:3000/booking for the first time, there was no error in the console( which also tricked me closing the issue ), but after that, when I started refreshing the page, the issue came up and stayed there. So I reopened the issue and wonder if there is a solution to fix it or maybe some hacky way so that I could just bypass this issue. Thanks in advance

michelengelen commented 2 months ago

Hey @MyNameIsTakenOMG I cannot reproduce this issue. But I can see that you have some plugins installed, right? This can sometimes lead to weird next js bugs and should disappear when in production. Try opening the page in a different browser or in privacy mode.

MyNameIsTakenOMG commented 2 months ago

@michelengelen Thank you for your prompt reply, Sir! And by following your advice, I tested it out in Firefox, and this time there was no issue as expected, and also more weirder thing is when I switched back to the google chrome and gave it try again, the issue was not there anymore whether I turned on/off those plugins, just like what happened yesterday.

It seems this is some issue that could occur intermittently while we develop our projects as developers. So for that, please allow me to share some workarounds I found, hopefully it could help someone else who may get stuck with this sort of hydration issue during development at some point in the future:

'use client'
import React, { useState } from 'react'
import { LocalizationProvider } from '@mui/x-date-pickers';
import { AdapterDayjs } from '@mui/x-date-pickers/AdapterDayjs'
// import TestCalendar from '@/components/TestCalendar';

import dynamic from 'next/dynamic';

const ClientCalendar1 = dynamic(() => import('@/components/TestCalendar'), { ssr: false })

const ClientCalendar2 = dynamic(() => import('@/components/TestCalendar'))

export default function Booking() {

  const [show, setShow] = useState(false)

  return (
    <LocalizationProvider dateAdapter={AdapterDayjs}>

      {/* only load at the client side */}
      <ClientCalendar1 />

      {/* load conditionally */}
      {show && <ClientCalendar2 />}
      <button onClick={() => setShow(!show)}>toggle</button>

      {/* <TestCalendar /> */}

    </LocalizationProvider>
  )
}

These workaround are Nextjs way to handle SSR, which basically help us skip SSR, so that it is ensured that we are 100% safe to use or test our components no matter what.

Okay, thanks again Sir! And my apology for any trouble caused by this wrong alarm. So I will close this issue now

github-actions[bot] commented 2 months ago

:warning: This issue has been closed. If you have a similar problem but not exactly the same, please open a new issue. Now, if you have additional information related to this issue or things that could help future readers, feel free to leave a comment.

@MyNameIsTakenOMG: How did we do? Your experience with our support team matters to us. If you have a moment, please share your thoughts in this short Support Satisfaction survey.