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

[TextField] Without id raise warnings with Next.js App Router #38103

Open
2 tasks done
tippfelher opened this issue Jul 22, 2023 · 5 comments
Open
2 tasks done

[TextField] Without id raise warnings with Next.js App Router #38103

tippfelher opened this issue Jul 22, 2023 · 5 comments
Labels
component: text field This is the name of the generic UI component, not the React module! docs Improvements or additions to the documentation external dependency Blocked by external dependency, we can’t do anything about it nextjs package: utils Specific to the @mui/utils package waiting for 👍 Waiting for upvotes

Comments

@tippfelher
Copy link

tippfelher commented Jul 22, 2023

Duplicates

  • I have searched the existing issues

Latest version

  • I have tested the latest version

Steps to reproduce 🕹

Here is the repository to reproduce the problem: https://github.com/tippfelher/nextjs13-app-dir-mui-textfield

import TextField from '@mui/material/TextField';

export default function Home() {
  return <TextField />;
}

Current behavior 😯

Warning: Prop id' did not match. Server: ": R219mcq:" Client: ": Ral6pj9:"

client and server generate random ids for the textfield which (obviously) never match and cause said error:

image

Expected behavior 🤔

Client and server generate the same id, and no warnings.

@tippfelher tippfelher added the status: waiting for maintainer These issues haven't been looked at yet by a maintainer label Jul 22, 2023
@oliviertassinari oliviertassinari added component: text field This is the name of the generic UI component, not the React module! package: material-ui Specific to @mui/material labels Jul 23, 2023
@oliviertassinari
Copy link
Member

oliviertassinari commented Jul 23, 2023

This is a bug in Next.js: vercel/next.js#48284.

@tippfelher
Copy link
Author

Thank you for the quick response. It looks like the issue described there. I.e. client and server trees don't match => useId() on client and useId() on server don't match => nothing mui can do here right? Feel free to close this issue then and sorry for the bug report. (:

@oliviertassinari oliviertassinari changed the title TextField without ID not working with React Server Components [TextField] Without id raise warnings with Next.js app Jul 23, 2023
@oliviertassinari oliviertassinari added external dependency Blocked by external dependency, we can’t do anything about it and removed status: waiting for maintainer These issues haven't been looked at yet by a maintainer labels Jul 23, 2023
@oliviertassinari oliviertassinari changed the title [TextField] Without id raise warnings with Next.js app [TextField] Without id raise warnings with Next.js appDir: true Jul 23, 2023
@oliviertassinari
Copy link
Member

oliviertassinari commented Jul 23, 2023

I can reproduce with Next.js v13.4.12 and:

TextField.tsx

'use client'
import * as React from "react";

export default function TextField() {
  const id = React.useId();
  return <div id={id} />;
}

nothing mui can do here right?

As a workaround, you can provide your own id to solve the hydration:

import TextField from '@mui/material/TextField';

<TextField id="my-id" />

Otherwise, we could have this workaround:

diff --git a/packages/mui-utils/src/useId/useId.ts b/packages/mui-utils/src/useId/useId.ts
index 6ea0324964..403b7b9a42 100644
--- a/packages/mui-utils/src/useId/useId.ts
+++ b/packages/mui-utils/src/useId/useId.ts
@@ -1,5 +1,6 @@
 'use client';
 import * as React from 'react';
+import ponyfillGlobal from '../ponyfillGlobal';

 let globalId = 0;
 function useGlobalId(idOverride?: string): string | undefined {
@@ -27,7 +28,11 @@ const maybeReactUseId: undefined | (() => string) = (React as any)['useId'.toStr
  * @returns {string}
  */
 export default function useId(idOverride?: string): string | undefined {
-  if (maybeReactUseId !== undefined) {
+  // Next.js app directory is buggy https://github.com/vercel/next.js/issues/48284
+  // ignore if ponyfillGlobal.next.appDir === true (client side)
+  // or ponyfillGlobal.__RSC_MANIFEST is defined (server side)
+  // eslint-disable-next-line no-underscore-dangle
+  if (maybeReactUseId !== undefined && ponyfillGlobal.next?.appDir !== true && !ponyfillGlobal.__RSC_MANIFEST) {
     const reactId = maybeReactUseId();
     return idOverride ?? reactId;
   }

but I don't know, feels like this is too Next.js specific, and that server-side, the id is still missing, so a warning help knowing where one is missing.

@oliviertassinari oliviertassinari changed the title [TextField] Without id raise warnings with Next.js appDir: true [TextField] Without id raise warnings with Next.js App Router Jul 23, 2023
@oliviertassinari oliviertassinari added package: utils Specific to the @mui/utils package and removed package: material-ui Specific to @mui/material labels Jul 23, 2023
@tippfelher
Copy link
Author

I think Material UI should NOT fix this, instead we should add a message to the Next.js App Router page. Similar to:

image

@oliviertassinari oliviertassinari added docs Improvements or additions to the documentation waiting for 👍 Waiting for upvotes labels Jul 23, 2023
@oliviertassinari
Copy link
Member

oliviertassinari commented Jul 23, 2023

Then I think we can use this GitHub issue as docs, wait for upvotes, and document it if it's raised often enough. We have so many issues open that it's not clear to me if it's worth the opportunity cost yet.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
component: text field This is the name of the generic UI component, not the React module! docs Improvements or additions to the documentation external dependency Blocked by external dependency, we can’t do anything about it nextjs package: utils Specific to the @mui/utils package waiting for 👍 Waiting for upvotes
Projects
None yet
Development

No branches or pull requests

3 participants