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

[examples] Updating the Link component example for the Next.js App Router #38746

Open
1 task done
Yedidya10 opened this issue Aug 31, 2023 · 5 comments
Open
1 task done
Assignees
Labels
examples Relating to /examples nextjs

Comments

@Yedidya10
Copy link

Yedidya10 commented Aug 31, 2023

Duplicates

  • I have searched the existing issues

Related page

https://github.com/mui/material-ui/blob/v5.14.7/examples/material-ui-nextjs-pages-router-ts/src/Link.tsx

Kind of issue

Missing information

Issue description

I thought that if I change the lines of code related to usePathname (due to the update in Next version 13) it will work,

...
import { usePathname } from 'next/navigation'
...

...
  const nextPathname = usePathname()
  const pathname = typeof href === 'string' ? href : href.pathname
  const className = clsx(classNameProps, {
    [activeClassName]: nextPathname === pathname && activeClassName,
  })
...

but I get 3 errors in the browser:

First 2 error messages:

Unhandled Runtime Error
Error: Hydration failed because the initial UI does not match what was rendered on the server.

Warning: Expected server HTML to contain a matching <a> in <a>.

See more info here: https://nextjs.org/docs/messages/react-hydration-error

Third error message:

Unhandled Runtime Error
Error: There was an error while hydrating. Because the error happened outside of a Suspense boundary, the entire root will switch to client rendering.

Context 🔦

It would be great if someone could update the example to work in the latest version.

@Yedidya10 Yedidya10 added status: waiting for maintainer These issues haven't been looked at yet by a maintainer support: docs-feedback Feedback from documentation page labels Aug 31, 2023
@sai6855 sai6855 self-assigned this Sep 1, 2023
@sai6855 sai6855 added examples Relating to /examples and removed status: waiting for maintainer These issues haven't been looked at yet by a maintainer support: docs-feedback Feedback from documentation page labels Sep 1, 2023
@sai6855
Copy link
Contributor

sai6855 commented Sep 1, 2023

I couldn't reproduce the issue. It's working fine on using usePathname hook. You can find the working code below. Please send a github repo which reproduces the issue you described

https://github.com/sai6855/material-ui/blob/examples-next-pages/examples/material-ui-nextjs-pages-router-ts/src/Link.tsx

@sai6855 sai6855 added the status: waiting for author Issue with insufficient information label Sep 1, 2023
@Yedidya10
Copy link
Author

I come across this in the repo:
https://github.com/Yedidya10/sharesphere

@github-actions github-actions bot removed the status: waiting for author Issue with insufficient information label Sep 1, 2023
@sai6855
Copy link
Contributor

sai6855 commented Sep 1, 2023

@Yedidya10 thanks for repo, please add steps for how to reproduce the issue

@sai6855 sai6855 added the status: waiting for author Issue with insufficient information label Sep 1, 2023
@Yedidya10
Copy link
Author

Yedidya10 commented Sep 1, 2023

@Yedidya10 thanks for repo, please add steps for how to reproduce the issue

For sure.

  1. Comment out the code snippet in: src\app\api\auth\[...nextauth]\route.ts page
GoogleProvider({
     clientId: getCredentials('GOOGLE').clientId,
     clientSecret: getCredentials('GOOGLE').clientSecret,
   }),
  1. Please create and add to the .env file:
NODE_ENV=development
NEXTAUTH_URL=http://127.0.0.1:3000
NEXTAUTH_SECRET=my_ultra_secure_nextauth_secret
MONGODB_URI_LOCAL=mongodb://127.0.0.1:27017/share_sphere
  1. Then, run a command: npm run dev

  2. Open the browser at http://127.0.0.1:3000/

And the errors will come up

@github-actions github-actions bot removed the status: waiting for author Issue with insufficient information label Sep 1, 2023
@mj12albert
Copy link
Member

Is it really because of usePathname?

I have opened a PR to update the Link component example for Next.js 13 but the only notable change is related to the child <a> and legacyBehavior prop: vercel/next.js#36436

@samuelsycamore samuelsycamore changed the title [docs] Updating the Link.tsx component example to version 13 (app directory) [examples] Updating the Link component example for the Next.js App Router Oct 23, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
examples Relating to /examples nextjs
Projects
None yet
Development

No branches or pull requests

4 participants